According to the PHP docs for pcntl_wait ,
The wait function pauses the execution of the current process until the child exits, or until a signal is delivered whose action is to terminate the current process or call the signal processing function.
However, when I run the following code and send SIGTERM to the parent process with kill -s SIGTERM [pid], the signal handler is called only after exiting it (i.e. I need to wait until sleep is complete. Should I not pcntl_wait()interrupt SIGTERM?
fork_test.php:
<?php
declare(ticks = 1);
function sig_handler($signo) {
switch ($signo) {
case SIGTERM:
echo 'SIGTERM' . PHP_EOL;
break;
default:
}
}
pcntl_signal(SIGTERM, 'sig_handler');
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
}
else if ($pid) {
echo 'parent' . PHP_EOL;
pcntl_wait($status);
}
else {
echo 'child' . PHP_EOL;
sleep(30);
}
?>
Exit (SIGTERM appears only after waiting 30 seconds):
$ php fork_test.php
child
parent
SIGTERM
PHP Version => 5.3.3