Workerman\Worker::daemonize PHP Method

daemonize() protected static method

Run as deamon mode.
protected static daemonize ( )
    protected static function daemonize()
    {
        if (!self::$daemonize) {
            return;
        }
        umask(0);
        $pid = pcntl_fork();
        if (-1 === $pid) {
            throw new Exception('fork fail');
        } elseif ($pid > 0) {
            exit(0);
        }
        if (-1 === posix_setsid()) {
            throw new Exception("setsid fail");
        }
        // Fork again avoid SVR4 system regain the control of terminal.
        $pid = pcntl_fork();
        if (-1 === $pid) {
            throw new Exception("fork fail");
        } elseif (0 !== $pid) {
            exit(0);
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Parse command.
  * php yourfile.php start | stop | restart | reload | status
  * @return void
  */
 protected static function parseCommand()
 {
     global $argv;
     // Check argv;
     $start_file = $argv[0];
     if (!isset($argv[1])) {
         exit("Usage: php yourfile.php {start|stop|restart|reload|status|kill}\n");
     }
     // Get command.
     $command = trim($argv[1]);
     $command2 = isset($argv[2]) ? $argv[2] : '';
     // Start command.
     $mode = '';
     if ($command === 'start') {
         if ($command2 === '-d') {
             $mode = 'in DAEMON mode';
         } else {
             $mode = 'in DEBUG mode';
         }
     }
     self::log("Workerman[{$start_file}] {$command} {$mode}");
     // Get master process PID.
     $master_pid = @file_get_contents(self::$pidFile);
     $master_is_alive = $master_pid && @posix_kill($master_pid, 0);
     // Master is still alive?
     if ($master_is_alive) {
         if ($command === 'start') {
             self::log("Workerman[{$start_file}] already running");
             exit;
         }
     } elseif ($command !== 'start' && $command !== 'restart') {
         self::log("Workerman[{$start_file}] not run");
     }
     // Execure command.
     switch ($command) {
         case 'kill':
             exec("ps aux | grep {$start_file} | grep -v grep | awk '{print \$2}' |xargs kill -SIGINT");
             exec("ps aux | grep {$start_file} | grep -v grep | awk '{print \$2}' |xargs kill -SIGKILL");
             break;
         case 'start':
             if ($command2 === '-d') {
                 Worker::$daemonize = true;
             }
             break;
         case 'status':
             if (is_file(self::$_statisticsFile)) {
                 @unlink(self::$_statisticsFile);
             }
             // Master process will send status signal to all child processes.
             posix_kill($master_pid, SIGUSR2);
             // Waiting amoment.
             usleep(100000);
             // Display statisitcs data from a disk file.
             readfile(self::$_statisticsFile);
             exit(0);
         case 'restart':
         case 'stop':
             self::log("Workerman[{$start_file}] is stoping ...");
             // Send stop signal to master process.
             $master_pid && posix_kill($master_pid, SIGINT);
             // Timeout.
             $timeout = 5;
             $start_time = time();
             // Check master process is still alive?
             while (1) {
                 $master_is_alive = $master_pid && posix_kill($master_pid, 0);
                 if ($master_is_alive) {
                     // Timeout?
                     if (time() - $start_time >= $timeout) {
                         self::log("Workerman[{$start_file}] stop fail");
                         exit;
                     }
                     // Waiting amoment.
                     usleep(10000);
                     continue;
                 }
                 // Stop success.
                 self::log("Workerman[{$start_file}] stop success");
                 if ($command === 'stop') {
                     exit(0);
                 }
                 if ($command2 === '-d') {
                     Worker::$daemonize = true;
                 }
                 break;
             }
             break;
         case 'reload':
             posix_kill($master_pid, SIGUSR1);
             self::log("Workerman[{$start_file}] reload");
             exit;
         default:
             exit("Usage: php yourfile.php {start|stop|restart|reload|status|kill}\n");
     }
 }
All Usage Examples Of Workerman\Worker::daemonize