private static function _daemonize($iSecondsToSleep = 10, $fHandler = null) { set_time_limit(0); declare (ticks=1); if (self::doesKeyExist("--do-not-daemonize") or self::doesKeyExist("--dnd")) { self::log("Daemonization is turned off. We will perform only one iteration of the main loop."); return; } $sPIDFile = self::getPIDFileName(); if (self::doesKeyExist("--kill")) { if (file_exists($sPIDFile) and is_readable($sPIDFile)) { $iPid = file_get_contents($sPIDFile); if (posix_kill($iPid, SIGTERM)) { self::log("SIGTERM was sent."); } else { self::log("Unable to send SIGTERM.", Daemon::PHP_MESSAGE); } if (is_writable($sPIDFile)) { unlink($sPIDFile); } else { self::log("Unable to unlink PID ({$sPIDFile}).", Daemon::PHP_MESSAGE); } } else { self::log("There is no PID ({$sPIDFile}).", Daemon::PHP_MESSAGE); } die; } $iChildPid = pcntl_fork(); if ($iChildPid) { /* shut down the parent process */ die; } /* the child process will be main */ posix_setsid(); /* sleep in order to parent process will be completely unloaded */ sleep(1); /* signal handler */ if (!$fHandler) { $fHandler = function ($iSignal) { switch ($iSignal) { case SIGTERM: Daemon::log("Daemon " . Daemon::getCurrentScriptBasename() . " has received SIGTERM."); Daemon::halt('SIGTERM'); break; } }; } if (!pcntl_signal(SIGTERM, $fHandler)) { self::log("Unable to set signal handler."); die; } if ($iSecondsToSleep < 0) { $iSecondsToSleep = 0; } self::$iMicroSecondsToSleep = $iSecondsToSleep * 1000000.0; }