friendica_2021.01_tupambae_.../bin/daemon.php

127 lines
2.4 KiB
PHP
Raw Normal View History

2017-12-14 17:38:51 +01:00
#!/usr/bin/env php
<?php
2016-11-27 23:52:21 +01:00
/**
2018-03-19 04:24:09 +01:00
* @file bin/daemon.php
2017-11-19 23:00:43 +01:00
* @brief Run the worker from a daemon.
2016-11-27 23:52:21 +01:00
*
* This script was taken from http://php.net/manual/en/function.pcntl-fork.php
*/
function shutdown() {
posix_kill(posix_getpid(), SIGHUP);
}
if (in_array("start", $_SERVER["argv"])) {
$mode = "start";
}
if (in_array("stop", $_SERVER["argv"])) {
$mode = "stop";
}
if (in_array("status", $_SERVER["argv"])) {
$mode = "status";
}
if (!isset($mode)) {
die("Please use either 'start', 'stop' or 'status'.\n");
}
2017-12-14 17:38:51 +01:00
if (empty($_SERVER["argv"][0])) {
die("Unexpected script behaviour. This message should never occur.\n");
}
// Fetch the base directory
$directory = dirname($_SERVER["argv"][0]);
if (substr($directory, 0, 1) != "/") {
$directory = $_SERVER["PWD"]."/".$directory;
}
$directory = realpath($directory."/..");
include $directory."/.htconfig.php";
if (!isset($pidfile)) {
die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
'$pidfile = "/path/to/daemon.pid";'."\n");
}
if (in_array($mode, array("stop", "status"))) {
$pid = @file_get_contents($pidfile);
if (!$pid) {
die("Pidfile wasn't found. Is the daemon running?\n");
}
}
if ($mode == "status") {
if (posix_kill($pid, 0)) {
die("Daemon process $pid is running.\n");
}
unlink($pidfile);
die("Daemon process $pid isn't running.\n");
}
if ($mode == "stop") {
posix_kill($pid, SIGTERM);
unlink($pidfile);
die("Worker daemon process $pid was killed.\n");
}
echo "Starting worker daemon.\n";
if (isset($a->config['php_path'])) {
$php = $a->config['php_path'];
} else {
$php = "php";
}
// Switch over to daemon mode.
if ($pid = pcntl_fork())
return; // Parent
fclose(STDIN); // Close all of the standard
fclose(STDOUT); // file descriptors as we
fclose(STDERR); // are running as a daemon.
register_shutdown_function('shutdown');
if (posix_setsid() < 0)
return;
if ($pid = pcntl_fork())
return; // Parent
$pid = getmypid();
file_put_contents($pidfile, $pid);
// Now running as a daemon.
while (true) {
2016-11-28 00:58:26 +01:00
// Just to be sure that this script really runs endlessly
set_time_limit(0);
2017-11-19 23:00:43 +01:00
// Call the worker
2018-03-19 04:25:21 +01:00
$cmdline = $php.' bin/worker.php';
2017-12-14 17:38:51 +01:00
$executed = false;
if (function_exists('proc_open')) {
$resource = proc_open($cmdline . ' &', array(), $foo, $directory);
if (is_resource($resource)) {
$executed = true;
proc_close($resource);
}
}
if (!$executed) {
exec($cmdline.' spawn');
}
// Now sleep for 5 minutes
sleep(300);
}