[DAEMONS] Switch daemons to double-forking for daemonisation

This commit is contained in:
Alexei Sorokin 2019-09-10 21:14:27 +03:00 committed by Diogo Peralta Cordeiro
parent 72f5576795
commit 1b66209e53

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()) {
case -1: // error
common_log(LOG_ERR, 'Could not fork.');
return false; return false;
} elseif ($pid > 0) { // parent case 0: // child
common_log(LOG_INFO, "Successfully forked."); if ($v === 'single') {
exit(0); posix_setsid();
} else { // child
return true;
} }
break;
default: // parent
if ($v === 'double') {
common_log(LOG_INFO, 'Successfully forked.');
}
die();
}
}
return true;
} }
public function alreadyRunning() public function alreadyRunning()