已监督运行 php 脚本的“未预期退出状态 1”


Supervisord "exit status 1 not expected" running php script

我在尝试配置主管以运行 php 脚本时遇到问题。在调试模式下运行主管给了我这个:

2015-03-09 08:53:06,342 INFO supervisord started with pid 2030
2015-03-09 08:53:06,358 INFO spawned: 'worker1' with pid 2031
2015-03-09 08:53:06,423 INFO exited: worker1 (exit status 1; not expected)
2015-03-09 08:53:06,424 INFO received SIGCLD indicating a child quit
2015-03-09 08:53:07,440 INFO spawned: 'worker1' with pid 2032
2015-03-09 08:53:07,587 INFO exited: worker1 (exit status 1; not expected)
2015-03-09 08:53:07,589 INFO received SIGCLD indicating a child quit
2015-03-09 08:53:09,604 INFO spawned: 'worker1' with pid 2033
2015-03-09 08:53:09,756 INFO exited: worker1 (exit status 1; not expected)
2015-03-09 08:53:09,758 INFO received SIGCLD indicating a child quit
2015-03-09 08:53:12,775 INFO spawned: 'worker1' with pid 2034
2015-03-09 08:53:12,973 INFO exited: worker1 (exit status 1; not expected)
2015-03-09 08:53:12,974 INFO received SIGCLD indicating a child quit
2015-03-09 08:53:13,976 INFO gave up: worker1 entered FATAL state, too many start retries too quickly

受监督配置:

[program:worker1]
command=php myScript.php
directory=/home/path/to/script/
user=root
autostart=true
autorestart=true
stderr_logfile=/var/log/worker1.err.log
stdout_logfile=/var/log/worker1.out.log
redirect_stderr=true
environment=PATH="/usr/bin"

对于此测试 myScript.php只需打印出echo "test".PHP_EOL;

没有日志报告来自 php 的错误,如果我通过 cli 运行脚本,它会按预期工作。主管日志仅报告与调试相同的输出。

我也尝试使用像/usr/bin/php /home/path/to/script/myScript.php这样的绝对路径,但没有任何变化。

myScript 的文件权限.php设置为 -rwxrwxr-x 1 root apache

真的不知道我还能检查什么。感谢您的支持!

UPDATE_1

我还尝试监视其他程序,例如/bin/cat 或 bash 脚本,并且像魅力一样工作。问题似乎仅限于 php。

UPDATE_2

正如 N.B. 在评论中指出的那样,我已经更改了测试脚本,使其看起来更像是长时间运行的作业:

while(true){
    echo "test".PHP_EOL;
    sleep(10);
}

和以前一样,它进入致命状态。

我可以使用退出代码 1 重现此行为的唯一方法是使用文件的无效路径。因此,首先请仔细检查路径是否正确。但我想你以前做过这件事。

我更假设该文件位于您的主目录下,并且 root 用户无法访问和运行它。所以我会尝试更改脚本的位置。

为了进行测试,您可以将脚本放在/tmp/myScript.php下:

cp /home/path/to/script/myScript.php /tmp/myScript.php

并像这样修改您的主管配置:

[program:worker1]
command=php myScript.php
directory=/tmp/
user=root
autostart=true
autorestart=true
stderr_logfile=/var/log/worker1.err.log
stdout_logfile=/var/log/worker1.out.log
redirect_stderr=true
environment=PATH="/usr/bin"

现在,受监督者应该能够运行您的脚本。

如果这有效,您应该检查哪个文件夹阻止 root 访问脚本。这可能是由加密(和挂载)文件夹(主目录?)引起的,或者如果该位置是从其他地方(桑巴舞、nfs ...)挂载的。要解决此问题,您可以尝试将用户从root更改为您的用户(我不建议这样做)或将项目位置更改为另一个文件夹,该文件夹不位于您的主目录下。

尝试设置startsecs = 0

[program:foo]
command = ls
startsecs = 0
autorestart = false
http://supervisord.org/configuration.html

启动秒

程序保持运行所需的总秒数 启动后要考虑启动成功。如果程序这样做 启动后不要熬夜这么多秒,即使它 以"预期"退出代码退出(请参阅退出代码),启动将 被视为失败。设置为 0 表示程序不需要 在任何特定的时间内保持运行。

我已经使用 Supervisord 一段时间了,这是我的配置中的内容:

[program:worker1]
command=php /absolute/path/to/myScript.php

我写这篇文章的原因是由于格式。

另外,请尝试以下脚本:

for(i = 0; i < 10; i++)
{
    printf("'nIteration: %d", $i);
    usleep(1000 * 200); // 200 milliseconds between each printf
}
exit;

并将其添加到监督中。它应该在执行回显后重新启动。

如果您像 docker -it exec ... 一样运行主管命令并得到exit status 1; not expected您应该尝试删除-it .更多内容请点击此处 https://stackoverflow.com/a/43099210/3664772