supervisor

about

install

直接使用使用yum命令即可直接安装即可:

yum install -y supervisor

生成supervisor的配置文件:

echo_supervisord_conf >  /etc/supervisord.conf

管理进程配置

可以在supervisor的配置文件中,添加如下内容。

[program:newcrm]
command=/opt/crm/crmenv/bin/uwsgi --ini /opt/crm/uwsgi.ini
autostart=true       ; 在supervisord启动的时候也自动启动
startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
stopasgroup=true     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=true     ;默认为false,向进程组发送kill信号,包括子进程
user=root

其它模板参考:

[program:mofang_celery_worker]
# 启动命令
command=/home/moluo/.virtualenvs/mofang/bin/celery worker -A manage.celery -P eventlet -l info -n worker1
# 项目根目录的绝对路径,通过pwd查看
directory=/home/moluo/Desktop/mofangapi
# 项目虚拟环境
enviroment=PATH="/home/moluo/.virtualenvs/mofang/bin"
# 输出日志绝对路径
stdout_logfile=/home/moluo/Desktop/mofangapi/logs/celery.worker.info.log
# 错误日志绝对路径
stderr_logfile=/home/moluo/Desktop/mofangapi/logs/celery.worker.error.log
# 自动启动
autostart=true
# 重启
autorestart=true
# 进程启动后跑了几秒钟,才被认定为成功启动,默认1
startsecs=10
# 进程结束后60秒才被认定结束
stopwatisecs=60
# 优先级
priority=997

常用命令

# 启动supervisor,默认会启动内部管理的进程
supervisord -c /etc/supervisord.conf   #启动supervisor服务端,指定配置文件启动

# 检查supervisor是否存在了进程,是否启动
ps -ef|grep  supervisord
ps aux | grep supervisord

# 杀掉supervisor进程
pkill -9 supervisord

# 重新加载配置信息
supervisorctl reload

# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
supervisorctl update  

# 设置supervisor开机自启
systemctl enable supervisord



# 停止某一个进程,program 就是进程名称,在ini文件首行定义的[program:mofang_celery_flower] 里的 :的名称
supervisorctl stop program

supervisorctl start program  # 启动某个进程
supervisorctl restart program  # 重启某个进程
supervisorctl stop groupworker:  # 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)
supervisorctl stop groupworker:name1  # 结束 groupworker:name1 这个进程 (start,restart 同理)
supervisorctl stop all  # 停止全部进程,注:start、restartUnlinking stale socket /tmp/supervisor.sock、stop 都不会载入最新的配置文件


# 进入supervisor任务管理终端
# 任务管理终端提供了这些命令
supervisorctl -c /etc/supervisord.conf

[root@cs crm]# supervisorctl -c /etc/supervisord.conf
newcrm                           STOPPED   May 02 02:42 PM
supervisor> start all
supervisor> start newcrm
supervisor> stop newcrm
supervisor> stop all
supervisor> status
supervisor> exit
[root@cs crm]# 

定期重启某个进程

# 我们可以利用crontab来实现定期任务,例如每小时重启进程abc
0 * * * * supervisorctl -c /etc/supervisord.conf restart abc

常见报错

unix:///tmp/supervisor.sock no such file

centos7.5

报错:

[root@cs ~]# supervisorctl -c /etc/supervisord.conf
unix:///tmp/supervisor.sock refused connection

默认的,这个报错是说sock文件找不到了,因为默认的,sock文件被保存在/tmp目录,而这个目录呢,根据规则默认是10天清理一次,所以,咱们的这个supervisor的socket文件很可能就被清理了。
解决办法有两种,第一种就是将supervisor的socket文件配置到别的目录去,这样就一劳永逸了。
还有种临时的解决办法就是重启supervisor:

# 看一下supervisor服务是否已经启动,启动的话就杀掉
[root@cs ~]# ps -ef|grep supervisord
root     23525     1  0 May20 ?        00:01:28 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     27000 26976  0 08:24 pts/0    00:00:00 grep --color=auto supervisord

# 杀掉
[root@cs ~]# pkill -9 supervisord

# 重启,成功了
[root@cs ~]# supervisord -c /etc/supervisord.conf
Unlinking stale socket /tmp/supervisor.sock
[root@cs ~]# ps -ef|grep supervisord
root     27066     1  1 08:28 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     27072 26976  0 08:29 pts/0    00:00:00 grep --color=auto supervisord

参考:https://blog.csdn.net/fdipzone/article/details/80146558

posted @ 2018-03-26 15:08  听雨危楼  阅读(264)  评论(0编辑  收藏  举报