1. 安装supervisor
pip install supervisor
2. 配置supervisor
2.1 生成默认配置文件
# 生成的配置文件可指定路径
echo_supervisord_conf > /etc/supervisord.conf
2.2 修改配置文件
最后一行添加
[program:celery]
;指定运行目录
directory=/root/my_prj
;运行目录下执行命令,日志保存在项目目录下
command=celery -A my_prj worker --loglevel info --logfile pro_celery_worker.log
;启动设置
numprocs=1 ;进程数
autostart=true ;当supervisor启动时,程序将会自动启动
autorestart=true ;自动重启
;停止信号,默认TERM
stopsignal=INT
如需启动多个,则新增一个块,比如下面
[program:pro.celery.worker]
;指定运行目录
directory=/root/my_prj1
;运行目录下执行命令,日志保存在项目目录下
command=celery -A my_prj1 worker --loglevel info --logfile pro_celery_worker.log
;启动设置
numprocs=1 ;进程数
autostart=true ;当supervisor启动时,程序将会自动启动
autorestart=true ;自动重启
;停止信号,默认TERM
stopsignal=INT
[program:dev.celery.worker]
;指定运行目录
directory=/root/my_prj2
;运行目录下执行命令,日志保存在项目目录下
command=celery -A my_prj2 worker --loglevel info --logfile dev_celery_worker.log
;启动设置
numprocs=1 ;进程数
autostart=true ;当supervisor启动时,程序将会自动启动
autorestart=true ;自动重启
;停止信号,默认TERM
stopsignal=INT
当然,也可以像Nginx一样配置指定路径下的配置文件集合
[include]
files = /etc/supervisor/conf.d/*.conf
注意:在需要同时启动worker和beat也需要添加整个Program,不能在一个program下使用两条command,如使用两条command,则只会执行最后一条
3. Supervisor常用命令
-
启动命令
# 指定配置文件启动 supervisord -c supervisord.conf
-
关闭命令
# 指定配置文件启动 supervisord -c supervisord.conf shutdown
-
重启命令
# 指定配置文件启动 supervisord -c supervisord.conf reload
-
重载配置文件
supervisorctl update supervisorctl reload
4. 常见错误
- 监听uwsgi时,uwsgi若配置了参数
daemonize=/var/log/uwsgi/%(project).log
,会导致supervisor监听失败。可使用supervisor指定uwsgi的日志输出,配置如下stdout_logfile=/var/logs/uwsgi.log ; stdout log path, NONE for none; default AUTO stdout_logfile_maxbytes=10MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=10