3 * @file util/daemon.php
4 * @brief Run the poller from a daemon.
6 * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
9 posix_kill(posix_getpid(), SIGHUP);
12 if (in_array("start", $_SERVER["argv"])) {
16 if (in_array("stop", $_SERVER["argv"])) {
20 if (in_array("status", $_SERVER["argv"])) {
25 die("Please use either 'start', 'stop' or 'status'.\n");
28 @include(".htconfig.php");
30 if (!isset($pidfile)) {
31 die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
32 '$pidfile = "/path/to/daemon.pid";'."\n");
35 if (in_array($mode, array("stop", "status"))) {
36 $pid = @file_get_contents($pidfile);
39 die("Pidfile wasn't found. Is the daemon running?\n");
43 if ($mode == "status") {
44 if (posix_kill($pid, 0)) {
45 die("Daemon process $pid is running.\n");
50 die("Daemon process $pid isn't running.\n");
53 if ($mode == "stop") {
54 posix_kill($pid, SIGTERM);
58 die("Worker daemon process $pid was killed.\n");
61 echo "Starting worker daemon.\n";
63 if (isset($a->config['php_path'])) {
64 $php = $a->config['php_path'];
69 // Switch over to daemon mode.
70 if ($pid = pcntl_fork())
73 fclose(STDIN); // Close all of the standard
74 fclose(STDOUT); // file descriptors as we
75 fclose(STDERR); // are running as a daemon.
77 register_shutdown_function('shutdown');
79 if (posix_setsid() < 0)
82 if ($pid = pcntl_fork())
86 file_put_contents($pidfile, $pid);
88 // Now running as a daemon.
90 // Just to be sure that this script really runs endlessly
94 $cmdline = $php.' include/poller.php';
98 // Now sleep for 5 minutes