Created
August 16, 2016 20:49
-
-
Save andreyserdjuk/34692d41188af16971092f2a33269708 to your computer and use it in GitHub Desktop.
pcntl_signal() handler usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(ticks = 1); | |
pcntl_signal(SIGTERM, 'signalHandler'); // Termination ('kill' was called) | |
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out | |
pcntl_signal(SIGINT, 'signalHandler'); // Interrupted (Ctrl-C is pressed) | |
$seconds = 0; | |
$total = 10; | |
while ($seconds < $total) { | |
sleep(1); | |
$seconds++; | |
echo sprintf('hi, %s seconds is left', $total - $seconds) . PHP_EOL; | |
}; | |
function signalHandler($signal) { | |
echo sprintf('ok, I\'m going with signal %s' . PHP_EOL, $signal); | |
global $seconds; | |
$seconds = 8; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment