kill 在触发pcntl_signal之前退出系统调用


kill exits system call before triggering pcntl_signal

我有一个我制作的守护程序脚本,当我ctrl-c时,我的信号被调用,但不在我的"系统"调用立即退出之前。我处理信号的全部目的是让"系统"调用在退出无限循环之前优雅地完成。如何阻止 kill 命令退出系统调用?

declare(ticks=1)
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);
    }
}

注意:如果在不分叉的情况下无法处理来自"系统"的信号,请告诉我,我将使用分叉。我避免分叉,因为 IMO 它使逻辑更加复杂,所以考虑到我的守护程序的串行性质,我认为避免它是明智的。

编辑:实现的ctrl-c与杀戮不同。如果我使用 kill,那么一切都按预期工作,但是如果我 ctrl-c,无论捕获信号如何,我的系统调用都会死亡(注意:信号仍然由 ctrl-c 触发)。

没有分叉是不可能的。