3

我从这里复制以将我的 Python 代码作为守护进程运行。额外的正常运行时间。我认为使用主管来保持这个守护进程运行会是一个更好的主意。

我这样做了。 python_deamon.conf

[program:python_deamon]
directory=/usr/local/python_deamon/
command=/usr/local/python_venv/bin/python daemon_runnner.py start
stderr_logfile=/var/log/gunicorn.log
stdout_logfile=/var/log/gunicorn.log
autostart=true
autorestart=true

问题是,尽管主管成功启动了 python_daemon,但它一直在重试。

2015-09-23 16:10:45,592 CRIT Supervisor running as root (no user in config file)
2015-09-23 16:10:45,592 WARN Included extra file "/etc/supervisor/conf.d/python_daemon.conf" during parsing
2015-09-23 16:10:45,592 INFO RPC interface 'supervisor' initialized
2015-09-23 16:10:45,592 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2015-09-23 16:10:45,592 INFO supervisord started with pid 13880
2015-09-23 16:10:46,595 INFO spawned: 'python_deamon' with pid 17884
2015-09-23 16:10:46,611 INFO exited: python_deamon (exit status 1; not expected)
2015-09-23 16:10:47,614 INFO spawned: 'python_deamon' with pid 17885
2015-09-23 16:10:47,630 INFO exited: python_deamon (exit status 1; not expected)
2015-09-23 16:10:49,635 INFO spawned: 'python_deamon' with pid 17888
2015-09-23 16:10:49,656 INFO exited: python_deamon (exit status 1; not expected)
2015-09-23 16:10:52,662 INFO spawned: 'python_deamon' with pid 17891
2015-09-23 16:10:52,680 INFO exited: python_deamon (exit status 1; not expected)
2015-09-23 16:10:53,681 INFO gave up: python_deamon entered FATAL state, too many start retries too quickly

只是为了记录后覆盖run()方法,我从不返回任何东西。

有可能做我想做的事还是我很笨?

PS:我知道整个问题的根本原因是因为 run() 永远不会返回任何东西,主管一直试图启动它,因此认为进程失败并给出状态为 FATAL Exited too quickly (process log may have details).

我的实际问题是我做得对吗?或者可以这样做吗?

PPS:独立(没有主管)daemon_runnner.py在有和没有 sudo 权限的情况下运行良好。

4

4 回答 4

5

尝试设置 startsecs = 0:

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

startsecs

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

于 2016-12-28T04:18:39.583 回答
2

这是我通常做的:

  • 创建一个service.conf描述新 Python 脚本的文件。该脚本引用了 shell 脚本,它实际上是启动 Python 脚本的脚本。这个.conf文件住在里面/etc/supervisor/conf.d/
  • 创建一个启动 Python 脚本的 shell 脚本。将权限更改为可执行文件。chmod 755 service.sh. 在这个脚本中,我们实际上启动了 Python 脚本。
  • 配置log_stderrstderr_logfile以验证问题。
  • 使用 reload 更新主管,然后检查状态:

主管>状态

alexad RUNNING pid 32657,正常运行时间 0:21:05

服务配置文件

[program:alexad]
; Set full path to celery program if using virtualenv

command=sh /usr/local/src/gonzo/supervisorctl/alexad.sh
directory=/usr/local/src/gonzo/services/alexa
log_stdout=true             ; if true, log program stdout (default true)
log_stderr=true             ; if true, log program stderr (default false)
stderr_logfile=/usr/local/src/gonzo/log/alexad.err
logfile=/usr/local/src/gonzo/log/alexad.log
autostart=true
autorestart=true
startsecs=10

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; Set Celery priority higher than default (999)
priority=500

服务.sh

#!/bin/bash
cd /usr/local/src/gonzo/services/alexa
exec python reader.py
于 2017-08-28T04:08:17.957 回答
0

您的脚本因退出状态而失败。主管只是试图重新启动脚本。

Supervisor 以 root 权限启动,也许它正在向您的脚本授予这些权限,这导致它失败(源目录中的更改或其他内容)。检查当您在没有主管的情况下以 root 身份运行守护程序时会发生什么。

我们真的需要更多信息才能知道它失败的原因。

于 2015-09-23T11:01:10.703 回答
-1

不确定问题是否与 daemon runner 相同,但如果您直接使用 daemon context 并使用 supervisord,则需要将 context.detach_process 设置为 False

于 2016-03-14T02:42:48.860 回答