[DAEMONS] Switch daemons to double-forking for daemonisation

This commit is contained in:
Alexei Sorokin 2019-09-10 21:14:27 +03:00
parent 9d8f4c774f
commit b0104d9992
1 changed files with 19 additions and 9 deletions

View File

@ -46,16 +46,26 @@ class Daemon
// Database connection will likely get lost after forking // Database connection will likely get lost after forking
$this->resetDb(); $this->resetDb();
$pid = pcntl_fork(); // Double-forking.
if ($pid < 0) { // error foreach (['single', 'double'] as $v) {
common_log(LOG_ERR, "Could not fork."); switch ($pid = pcntl_fork()) {
return false; case -1: // error
} elseif ($pid > 0) { // parent common_log(LOG_ERR, 'Could not fork.');
common_log(LOG_INFO, "Successfully forked."); return false;
exit(0); case 0: // child
} else { // child if ($v === 'single') {
return true; posix_setsid();
}
break;
default: // parent
if ($v === 'double') {
common_log(LOG_INFO, 'Successfully forked.');
}
die();
}
} }
return true;
} }
public function alreadyRunning() public function alreadyRunning()