Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.