杀戮被"系统"吸收。调用并且永远不会触发pcntl_signal

时间:2016-03-08 21:24:25

标签: php pcntl

我有一个守护程序脚本,我试图通过避免线程来保持简单。

当我按ctrl-c或杀死pid时,"系统"呼叫已退出,但exitFunction未被调用。

declare(ticks = 100);

function exitFunction($signo) {
    global $pidFile, $exit;
    unlink($pidFile);
    echo "Daemon is exiting (signal: $signo). Removing pidFile: $pidFile\n";
    $exit = true;
};
//create the signal handler and shutdown function
pcntl_signal(SIGINT, "exitFunction");
pcntl_signal(SIGTERM, "exitFunction");

//create the pid file with our command
file_put_contents($pidFile, posix_getpid());

echo "pid: " . posix_getpid() . "\n";
echo "Time to start!\n";
while(!$exit) {
    echo "running $command...\n";
    system($command, $return);
    echo "done $command\n";
    if($return) {
        echo "didn't find any domains with command, so sleeping for 60...\n";
        sleep(60);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在脚本的第一行添加declare(ticks=1);。可以在here找到更详细的实际蜱的解释。