Search code examples
phppcntl

kill is absorbed by "system" call and never triggers pcntl_signal


I have a daemon script and I'm trying to keep it simple by avoiding threads.

When i ctrl-c or kill pid, the "system" call is exited, but exitFunction isn't called.

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);
    }
}

Solution

  • You'll need to add declare(ticks=1); as the first line in your script. A more detailed explanation of what ticks actually are can be found here.