Skip to content

Instantly share code, notes, and snippets.

@andreyserdjuk
Created August 16, 2016 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreyserdjuk/34692d41188af16971092f2a33269708 to your computer and use it in GitHub Desktop.
Save andreyserdjuk/34692d41188af16971092f2a33269708 to your computer and use it in GitHub Desktop.
pcntl_signal() handler usage example
<?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