Created
June 24, 2015 07:20
-
-
Save cillierburger/3224621f82610ba92566 to your computer and use it in GitHub Desktop.
This file contains hidden or 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); | |
echo "simple daemon\n"; | |
// tick use required as of PHP 4.3.0 | |
// signal handler function | |
function sig_handler($signo) | |
{ | |
switch ($signo) { | |
case SIGTERM: | |
echo "Cleaning up\n"; | |
echo "Done,exiting\n"; | |
// handle shutdown tasks | |
exit; | |
break; | |
case SIGHUP: | |
echo "Reloading configuration\n"; | |
// handle restart tasks | |
break; | |
case SIGUSR1: | |
echo "Caught SIGUSR1...\n"; | |
echo "I can do whatever i want with sigusr\n"; | |
break; | |
default: | |
// handle all other signals | |
} | |
} | |
echo "Installing signal handler...\n"; | |
// setup signal handlers | |
pcntl_signal(SIGTERM, "sig_handler"); | |
pcntl_signal(SIGHUP, "sig_handler"); | |
pcntl_signal(SIGUSR1, "sig_handler"); | |
$pid = pcntl_fork(); | |
if ($pid == 0 ) { | |
echo "I'm in the child!\n"; | |
while(true) { | |
sleep(5); | |
echo "I'm still running...\n"; | |
} | |
} | |
else { | |
echo "child pid is $pid, exiting\n"; | |
die(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment