/** * Put the running script in background * * @return void */ protected static function _summon() { if (self::opt('usePEARLogInstance')) { $logLoc = '(PEAR Log)'; } else { if (self::opt('useCustomLogHandler')) { $logLoc = '(Custom log handler)'; } else { $logLoc = self::opt('logLocation'); } } self::notice('Starting {appName} daemon, output in: %s', $logLoc); // Allowed? if (self::isRunning()) { return self::emerg('{appName} daemon is still running. Exiting'); } // Reset Process Information self::$_safeMode = !!@ini_get('safe_mode'); self::$_processId = 0; self::$_processIsChild = false; // Fork process! if (!self::_fork()) { return self::emerg('Unable to fork'); } // Additional PID succeeded check if (!is_numeric(self::$_processId) || self::$_processId < 1) { return self::emerg('No valid pid: %s', self::$_processId); } // Change umask @umask(0); // Write pidfile $p = self::_writePid(self::opt('appPidLocation'), self::$_processId); if (false === $p) { return self::emerg('Unable to write pid file {appPidLocation}'); } // Change identity. maybe $c = self::_changeIdentity(self::opt('appRunAsGID'), self::opt('appRunAsUID')); if (false === $c) { self::crit('Unable to change identity'); if (self::opt('appDieOnIdentityCrisis')) { self::emerg('Cannot continue after this'); } } // Important for daemons // See http://www.php.net/manual/en/function.pcntl-signal.php declare (ticks=1); // Setup signal handlers // Handlers for individual signals can be overrulled with // setSigHandler() foreach (self::$_sigHandlers as $signal => $handler) { if (!is_callable($handler) && $handler != SIG_IGN && $handler != SIG_DFL) { return self::emerg('You want to assign signal %s to handler %s but ' . 'it\'s not callable', $signal, $handler); } else { if (!pcntl_signal($signal, $handler)) { return self::emerg('Unable to reroute signal handler: %s', $signal); } } } // Change dir @chdir(self::opt('appDir')); return true; }
/** * Put the running script in background * * @return void */ protected static function _summon() { self::log(self::LOG_NOTICE, "starting " . self::getOption("appName") . " " . "daemon, output in: " . self::getOption("logLocation"), __FILE__, __CLASS__, __FUNCTION__, __LINE__); // Allowed? if (self::isRunning()) { self::log(self::LOG_EMERG, "" . self::getOption("appName") . " " . "daemon is still running. " . "exiting", __FILE__, __CLASS__, __FUNCTION__, __LINE__); } // Reset Process Information self::$_safeMode = (bool) @ini_get("safe_mode") === false ? false : true; self::$_processId = 0; self::$_processIsChild = false; // Fork process! if (!self::_fork()) { self::log(self::LOG_EMERG, "" . self::getOption("appName") . " " . "daemon was unable to fork", __FILE__, __CLASS__, __FUNCTION__, __LINE__); } // Additional PID succeeded check if (!is_numeric(self::$_processId) || self::$_processId < 1) { self::log(self::LOG_EMERG, "" . self::getOption("appName") . " " . "daemon didn't have a valid " . "pid: '" . self::$_processId . "'", __FILE__, __CLASS__, __FUNCTION__, __LINE__); } // Change umask @umask(0); // Write pidfile if (false === self::_writePid(self::getOption("appPidLocation"), self::$_processId)) { self::log(self::LOG_EMERG, "" . self::getOption("appName") . " " . "daemon was unable " . "to write pid file"); } // Change identity. maybe if (false === self::_changeIdentity(self::getOption("appRunAsGID"), self::getOption("appRunAsUID"))) { // Die on fail? $lvl = self::getOption("appDieOnIdentityCrisis") ? self::LOG_EMERG : self::LOG_CRIT; self::log($lvl, "" . self::getOption("appName") . " " . "daemon was unable " . "to change identity"); } // Important for daemons // See http://www.php.net/manual/en/function.pcntl-signal.php declare (ticks=1); // Setup signal handlers // Handlers for individual signals can be overrulled with // setSigHandler() foreach (self::$_sigHandlers as $signal => $handler) { if (!pcntl_signal($signal, $handler)) { self::log(self::LOG_EMERG, "" . self::getOption("appName") . " " . "daemon was unable " . "to reroute signal handler " . $signal); return false; } } // Change dir @chdir(self::getOption("appDir")); return true; }