Run a python script with supervisor

时间:2015-09-23 10:45:02

标签: python daemon supervisord python-daemon

I copied from here以将我的Python代码作为守护程序运行。 额外的正常运行时间。我认为使用supervisor来保持这个守护进程运行会更好。

我这样做了。 的 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()永远不会返回任何主管一直试图启动它并因此认为该过程失败并将状态设为{ {1}}。

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

P.P.S :独立(没有主管)FATAL Exited too quickly (process log may have details)运行正常,有或没有sudo权限。

4 个答案:

答案 0 :(得分:3)

尝试设置startsecs = 0:

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

startsecs

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

答案 1 :(得分:0)

您的脚本失败并退出状态。主管只是试图重启脚本。

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

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

答案 2 :(得分:0)

这就是我通常做的事情:

  • 创建一个描述新Python脚本的service.conf文件。这个脚本引用了shell脚本,这是实际启动Python脚本的脚本。此.conf文件位于/etc/supervisor/conf.d
  • 创建一个启动Python脚本的shell脚本。更改可执行文件的权限chmod 755 service.sh。在这个脚本中,我们实际上启动了Python脚本。
  • 配置 log_stderr stderr_logfile 以验证问题。
  • 使用重新加载更新主管,然后检查状态:
  

监>状态

     

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

<强> service.conf

[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

<强> service.sh

#!/bin/bash
cd /usr/local/src/gonzo/services/alexa
exec python reader.py

答案 3 :(得分:-1)

不确定问题是否与守护程序运行程序相同,但如果直接使用守护程序上下文并使用supervisord,则需要将context.detach_process设置为False