PHP实现守护进程的方式

nohup

nohup命令 可以将程序以忽略挂起信号的方式运行起来,被运行的程序的输出信息将不会显示到终端。

无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out 文件不可写,输出重定向到$HOME/nohup.out文件中。如果没有文件能创建或打开以用于追加,那么 command 参数指定的命令不可调用。如果标准错误是一个终端,那么把指定的命令写给标准错误的所有输出作为标准输出重定向到相同的文件描述符。

语法

nohup(选项)(参数)

选项

--help:在线帮助;
--version:显示版本信息。

参数

程序及选项:要运行的程序及选项。

实例

使用nohup命令提交作业,如果使用nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,除非另外指定了输出文件:

nohup command > myout.file 2>&1 &

在上面的例子中,输出被重定向到myout.file文件中。

该指令表示不做挂断操作,后台下载

nohup wget site.com/file.zip

下面命令,会在同一个目录下生成一个名称为 nohup.out 的文件,其中包含了正在运行的程序的输出内容

nohup ping -c 10 baidu.com

PHP示例

nohup xxx.php &

使用pcntl和posix扩展实现

PHP 实现守护进程

  • fork 子进程
  • 父进程退出
  • 设置新的会话
  • 重置文件掩码
  • 关闭标准输入输出
<?php
// Fork the current process
$pid = pcntl_fork();

// If pid is negative, an error occurred
if ($pid == -1) {
    exit("Error forking...\n");
} 
// If pid is 0, this is the child process
elseif ($pid === 0) {
    // Make the child process a new session leader
    if (posix_setsid() === -1) {
        exit("Error creating new session...\n");
    }
    
    $dir = '../../../vendor/mydaemon.log';
    // Open a log file for writing
    $log = fopen($dir, 'w');

    // Loop indefinitely
    while (true) {
        // Write a message to the log file
        fwrite($log, "Daemon is running...\n");

        // Sleep for 5 seconds
        sleep(5);
    }
    
    //close the standard input, output, and error streams respectively.
    //REF:https://www.php.net/manual/zh/wrappers.php.php
    //REF:https://www.php.net/manual/zh/features.commandline.io-streams.php

    fclose(STDIN);
    fclose(STDOUT);
    fclose(STDERR);
} 
// If pid is positive, this is the parent process
else {
    // Exit the parent process
    exit('Exit the parent process');
}