运行 PHP 文件在 Linux 服务器后台用于推送通知

Run PHP file in background of Linux server for Push notifications

我正在尝试在 Linux 服务器的后台执行一个 PHP 文件,使用在博客上找到的很酷的脚本:

NAME=servicename
DESC="Daemon for my magnificent PHP CLI script"

DAEMON="/usr/bin/php"
DAEMON_OPTS="/var/www/html/phpscript.php"

test -x $DAEMON || exit 0

set -e

case "" in
    start)
        echo -n "Starting ${DESC}: "
        nohup php $DAEMON_OPTS &
        ;;
    stop)
        ps faux | grep php
        echo -n "Can't Stop process, use kill -9 pid"
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop}" >&2
        exit 1
        ;;
esac

exit 0

我将 shell 脚本创建到 /etc/init.d/

执行

chmod +x phpscript.php

...和

update-rc.d servicename defaults

命令,但在

启动脚本后
/etc/init.d/servicename start

...,服务过一会就停止了。不知道为什么。

在这里,phpscript :

[...]
function start(){
            while (1) {
                    [...]
                    if (mysqli_num_rows($result) > 0) {
                            $this->checkAPNS();
                            if ($this->fp) {
                                    while ($data = mysqli_fetch_array($result)) {
                                            $token = $data[0];
                                            $payload = $data[1];
                                            $sendPush = sendPush($this->fp, $token, $payload);
                                            if (!$sendPush) {
                                                    $retry = 0;
                                                    while (1) {
                                                            if ($this->reconnectToAPNS()) {
                                                                    return;
                                                            }
                                                            if ($retry++ > 3) {
                                                                    file_put_contents("push.log", "Error at ".msTimeStamp()."\n", FILE_APPEND | LOCK_EX);
                                                                    sleep(10);
                                                                    return;
                                                            }
                                                    }
                                            }
                                            [...]
                                    }
                            }

                    usleep(500000);
                    }
            }
    }
[...]

我怎样才能保持这个过程运行?

终止进程的原因几乎无穷无尽。任何未捕获的未处理异常或运行时错误都将导致您的进程退出。如果您的 mysql 连接已打开一段时间,那么每当您尝试查询数据库时,都会收到可怕的 "MySQL server has gone away" 错误。

如果您不注意分配资源的方式,那么一段时间后您可能会填满机器上的所有可用内存并导致 PHP 发出 "Allowed memory size of x bytes exhausted"

根据您的 OS,您可以设置 PHP 将错误从命令行环境输出到特定的日志文件。听起来您不太确定问题是什么,所以这应该是您的第一步。 Find the php.ini file being loaded by the command line PHP then find the configuration setting for error_log 并检查引用的文件以确定您的脚本终止的原因。如果您希望此特定脚本在其他地方记录错误,您也应该能够通过 ini_set() 更新它。

作为后备补救措施,您可以使用进程管理软件,例如 supervisord,它作为监视特定进程的服务运行。如果它们因任何原因退出或退出,则主管将使用可配置参数自动重新启动它们。

最后一点,您应该确保在可能抛出异常的地方包含 try/catch 块。我在您的缩写代码中没有看到任何内容,因此它们可能已经存在,只是您没有共享。未处理的异常 终止您的脚本,因此我总是至少在最外层范围中包含 try/catch 以确保捕获所有异常。