/** * Respond to a signal * * @param $signal */ public function handleSignals($signal) { $signame = array_search($signal, $this->signals); if (!$signame) { $signame = 'UNKNOWN'; } if (!SignalHandler::$blockExit) { Logger::critical(sprintf('Exiting : %s signal received', $signame)); exit; } else { Logger::warning(sprintf('%s signal received : Exit queued', $signame)); SignalHandler::$forceExit = true; } }
/** * Exits this process if there is already one running (this one makes 2) * * @return void */ protected function thereCanBeOnlyOne() { $controller = $this->__toString(); $parts = preg_split('/[^a-zA-Z0-9]{1,}/', $controller); $regex = join('[^a-zA-Z0-9]{1,2}', $parts); $cmd = 'ps ax | grep -v grep | egrep -c "' . $regex . '"'; $res = `{$cmd}`; if (intval($res) > 1) { Logger::critical('Unable to launch : Process already running'); exit(1); } }
/** * Daemonises the process */ protected function fork_to_bg() { $daemon_pid = pcntl_fork(); switch ($daemon_pid) { case -1: Logger::emergency('Unable to fork daemon process'); exit(1); // fork failed // fork failed case 0: // this child is our daemon $this->process_pid = getmypid(); break; default: // we are the parent - the one from the FG command line // return control to command line by exiting... \Cli\line('Daemon process running : pid=%y' . $daemon_pid . '%n'); exit(0); } // promote the daemon process so it doesn't die because the parent has if (posix_setsid() === -1) { Logger::critical('Error creating daemon as session leader'); exit(1); } fclose(STDIN); fclose(STDOUT); fclose(STDERR); $this->_stdIn = fopen('/dev/null', 'r'); // set fd/0 $this->_stdOut = fopen('/dev/null', 'w'); // set fd/1 $this->_stdErr = fopen('php://stdout', 'w'); // a hack to duplicate fd/1 to 2 // Silence any console output from the logger Logger::setSilentConsole(true); Logger::notice('Daemon process running : pid=' . getmypid()); }