Example #1
0
 /**
  * Fork the process into two
  * You can create an optional method called preFork() which is called before the fork
  * Also an optional method called postFork() which is called after the fork on any remaining processes
  * (ie if you choose to daemonize then the original foreground process will not call the postFork() method)
  *
  * @param bool $daemonize kill the parent (original) process and let the new child run
  *
  * @return void
  */
 protected function fork($daemonize = false)
 {
     if ($this->node !== 0) {
         return false;
     }
     // call any user created preFork method
     if (method_exists($this, 'preFork')) {
         $this->preFork();
     }
     // force Propel to close connections so that it will reconnect on next query
     if (class_exists('\\Propel')) {
         \Propel::close();
     }
     $pid = pcntl_fork();
     switch ($pid) {
         case -1:
             Logger::emergency('Unable to fork process');
             return;
         case 0:
             // this is the child
             $this->node++;
             break;
         default:
             // we are the original process
             if ($this->node == 0) {
                 \Cli\line('Child node : pid=%y%s%n', $pid);
                 if ($daemonize) {
                     //                        exit;
                 } else {
                     $this->pid[] = $pid;
                 }
                 return;
             }
     }
     // promote the daemon process so it doesn't die because the parent has
     if ($daemonize && 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);
     // call any user created postFork method
     if (method_exists($this, 'postFork')) {
         $this->postFork();
     }
 }
Example #2
0
 /**
  * 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());
 }