Skip to content

Instantly share code, notes, and snippets.

@marinsagovac
Last active December 14, 2017 10:16
Show Gist options
  • Save marinsagovac/10f29be99f471dc40155 to your computer and use it in GitHub Desktop.
Save marinsagovac/10f29be99f471dc40155 to your computer and use it in GitHub Desktop.
PCNTL fork process
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
exit(); // we are the parent
} else {
// we are the child
}
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
while (1)
{
}
function sig_handler($signo)
{
switch ($signo) {
case SIGTERM:
// handle shutdown tasks
exit;
break;
case SIGHUP:
// handle restart tasks
break;
default:
// handle all other signals
echo time();
}
}
var_dump($pid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment