php-signal-handler
php-signal-handler copied to clipboard
Singnal handling based on C signal for blocking/nonblocking php scripts
PHP Signal Handler
Extension uses C signal function (from <signal.h>) to call previously defined callback, if appropriate signal has arrived (i.e SIGUSR1).
When should I use it?
Could be used with php-amqp to print the actual worker/consumer status (ie. message stats, last operation performed etc.)
What's more it is possible to "catch" a unix signal i.e. SIGTERM and then kill the process nicely (program won't stop untill all operations are completed). It is very useful if script operates on sensitive data which cannot be damaged.
Why don't you use pcntl_signal?
If a blocking method (such as AMQPQueue::consume()
from php-amqp) is holding the processing thread, then you won't be able to call pcntl_signal_dispatch() and trigger the callbacks registered with pcntl_signal().
Compatibility
Extension will work only if C function signal
is available (only on UNIX systems).
PHP versions supported:
- PHP 7
- PHP 5.6
- PHP 5.5
- PHP 5.4
- PHP 5.3
Example
<?php
function getInfo($noStatus){
echo "getInfo called with status $noStatus\n";
}
$r = attach_signal(10, 'getInfo');
while(true){}
?>
Spawn the process:
php your_script_name.php
Kill the first process from the list:
kill -s SIGUSR1 `ps -ef | grep php | head -n1 | awk '{print $2}'`
After that steps you should see:
getInfo called with status 10