Supervisor安装及使用

supervisor 是基于 python 的任务管理工具,用来自动运行各种后台任务,当然你也能直接利用 nohup 命令使任务自动后台运行,但如果要重启任务,每次都自己手动 kill 掉任务进程,这样很繁琐,而且一旦程序错误导致进程退出的话,系统也无法自动重载任务。


 

由于supervisor在python3下无法使用,因此只能用python2去下载!!!!!!

yum install python-setuptools
easy_install supervisor

 

通过命令生成supervisor的配支文件

echo_supervisord_conf > /etc/supervisord.conf

编辑所需管理的项目

vi /etc/supervisord.conf

在/etc/supervisord.conf末尾添加上如下代码

[program:mosquitto]
command=mosquitto -v 
stopasgroup=true      
killasgroup=true 
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true

[program:redis]
command=redis-server  /usr/local/redis-4.0.6/redis.conf
stopasgroup=true      
killasgroup=true 
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true

[program:ipm2020]
command=/usr/local/python3/bin/uwsgi --ini /opt/uwsgi/uwsgi.ini
stopasgroup=true      
killasgroup=true 
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true

[program:celery]
directory=/opt/djcelery/mqtt/
command=/root/Envs/ipm/bin/celery -A mycelery.main worker -l info ;使用了虚拟环境的celery
stopasgroup=true      
killasgroup=true 
autostart = true     ; 在 supervisord 启动的时候也自动启动
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true

运行supervisord,查看是否生效

supervisord -c /etc/supervisor/supervisord.conf

配置Supervisor开机启动

新建一个“supervisord.service”文件

vi /usr/lib/systemd/system/supervisord.service

内容如下

 

# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target

 

执行命令

 

systemctl enable supervisord

 

常用的相关管理命令

supervisorctl restart <application name> ;重启指定应用
supervisorctl stop <application name> ;停止指定应用
supervisorctl start <application name> ;启动指定应用
supervisorctl restart all ;重启所有应用
supervisorctl stop all ;停止所有应用
supervisorctl start all ;启动所有应用

 

 

 

posted @ 2019-11-21 11:40  丹尼伯夷  阅读(314)  评论(0编辑  收藏  举报