Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统,这篇文章主要介绍了python测试开发django之使用supervisord 后台启动celery 服务(worker/beat),需要的朋友可以参考下

前言
Supervisor(‘http://supervisord.org/’)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
它可以很方便的监听、启动、停止、重启一个或多个进程。
用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

环境准备
centos 安装 supervisord

yum install -y epel-release && yum install -y supervisor

安装完成后在/etc/supervisor 目录下会有个配置文件 supervisord.conf
在项目中我们需要用到此配置,复制一份到项目根目录
supervisord.conf文件内容编写, 前面内容不用改,直接接着在后面写
比如我需要后台启动celery的worker和beat服务

; Sample supervisor config file. ; ; For more information on the config file, please see: ; http://supervisord.org/configuration.html   ; 前面文档省略,不用删,接着后面写   [program:celery-worker] command=celery -A hrun2_web worker -l info ; 管理命令,supervisor不支持后台进程 process_name=%(program_name)s #user=root                                 ;进程启动用户 autostart=true                           ;是否随supervisor启动 autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉! startretries=3                           ;启动尝试次数 stderr_logfile=./celery_worker.log           ;标准输出文件 stdout_logfile=./celery_worker.log        ;标准输出文件 loglevel=info                            ;日志的级别     [program:celery-beat] command=celery -A hrun2_web beat ; 管理命令,supervisor不支持后台进程 process_name=%(program_name)s #user=root                                 ;进程启动用户 autostart=true                           ;是否随supervisor启动 autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉! startretries=3                           ;启动尝试次数 stderr_logfile=./celery_beat.log          ;标准输出文件 stdout_logfile=./celery_beat.log        ;标准输出文件 loglevel=info                            ;日志的级别 

启动服务

supervisord -c /path/supervisord.conf

关闭服务

supervisorctl shutdown

查看启动的服务

supervisorctl status

执行结果如下

root@13107c465557:/code# supervisorctl status celery-worker RUNNING pid 234, uptime 0:00:14 celery-beat RUNNING pid 235, uptime 0:00:14 

说明启动了celery和celery-beat两个服务

查看日志
我们在配置里面指定了./celery_worker.log 文件保存运行日志,所以可以直接查看这个文件

tail -f celery_worker.log -n 30