]> git.mxchange.org Git - friendica.git/blob - scripts/daemon.php
Small fixes to translatable string
[friendica.git] / scripts / daemon.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * @file scripts/daemon.php
5  * @brief Run the worker from a daemon.
6  *
7  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
8  */
9 function shutdown() {
10         posix_kill(posix_getpid(), SIGHUP);
11 }
12
13 if (in_array("start", $_SERVER["argv"])) {
14         $mode = "start";
15 }
16
17 if (in_array("stop", $_SERVER["argv"])) {
18         $mode = "stop";
19 }
20
21 if (in_array("status", $_SERVER["argv"])) {
22         $mode = "status";
23 }
24
25 if (!isset($mode)) {
26         die("Please use either 'start', 'stop' or 'status'.\n");
27 }
28
29 if (empty($_SERVER["argv"][0])) {
30         die("Unexpected script behaviour. This message should never occur.\n");
31 }
32
33 // Fetch the base directory
34 $directory = dirname($_SERVER["argv"][0]);
35
36 if (substr($directory, 0, 1) != "/") {
37         $directory = $_SERVER["PWD"]."/".$directory;
38 }
39 $directory = realpath($directory."/..");
40
41 @include($directory."/.htconfig.php");
42
43 if (!isset($pidfile)) {
44         die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
45                 '$pidfile = "/path/to/daemon.pid";'."\n");
46 }
47
48 if (in_array($mode, array("stop", "status"))) {
49         $pid = @file_get_contents($pidfile);
50
51         if (!$pid) {
52                 die("Pidfile wasn't found. Is the daemon running?\n");
53         }
54 }
55
56 if ($mode == "status") {
57         if (posix_kill($pid, 0)) {
58                 die("Daemon process $pid is running.\n");
59         }
60
61         unlink($pidfile);
62
63         die("Daemon process $pid isn't running.\n");
64 }
65
66 if ($mode == "stop") {
67         posix_kill($pid, SIGTERM);
68
69         unlink($pidfile);
70
71         die("Worker daemon process $pid was killed.\n");
72 }
73
74 echo "Starting worker daemon.\n";
75
76 if (isset($a->config['php_path'])) {
77         $php = $a->config['php_path'];
78 } else {
79         $php = "php";
80 }
81
82 // Switch over to daemon mode.
83 if ($pid = pcntl_fork())
84         return;     // Parent
85
86 fclose(STDIN);  // Close all of the standard
87 fclose(STDOUT); // file descriptors as we
88 fclose(STDERR); // are running as a daemon.
89
90 register_shutdown_function('shutdown');
91
92 if (posix_setsid() < 0)
93         return;
94
95 if ($pid = pcntl_fork())
96         return;     // Parent
97
98 $pid = getmypid();
99 file_put_contents($pidfile, $pid);
100
101 // Now running as a daemon.
102 while (true) {
103         // Just to be sure that this script really runs endlessly
104         set_time_limit(0);
105
106         // Call the worker
107         $cmdline = $php.' scripts/worker.php';
108
109         $executed = false;
110
111         if (function_exists('proc_open')) {
112                 $resource = proc_open($cmdline . ' &', array(), $foo, $directory);
113
114                 if (is_resource($resource)) {
115                         $executed = true;
116                         proc_close($resource);
117                 }
118         }
119
120         if (!$executed) {
121                 exec($cmdline.' spawn');
122         }
123
124         // Now sleep for 5 minutes
125         sleep(300);
126 }