Ubuntu 安装配置 supervisor
背景交代:
一台 Job queue 服务器,Ubuntu 18.04, 上面跑着 Beanstalkd 服务 + PHP 7.2 环境,运行着几个 php 的命令行脚本,作为 consumer 去消费任务,一直以 nohup & 的方式去运行,实践证明,这种方式并不可靠,脚本还是会死掉,那就上 supervisor 保平安吧。
Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,假如你有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断。当进程中断的时候我希望能自动重新启动它,这就是 supervisor 做的事情。
安装步骤
Ubuntu 安装:直接用 apt 方式安装,简单省事,开机自启动都自动配好了。
sudo apt-get install supervisor
CentOS 安装
yum install supervisor -y
/etc/supervisord.d/*.ini 配置文件
安装之后一般都改为 *.conf
查看版本号:
supervisord -v
3.3.1
Config
/* 主配置文件 */
/etc/supervisor/supervisord.conf
/* 子配置文件 */
/etc/supervisor/conf.d/*.conf
新建配置文件,建议每个任务一个独立的配置文件
cd /etc/supervisor/conf.d
vim googlehome_subscribe.conf
配置文件内容
[program:googlehome_subscribe]
directory = /www/wwwroot/google-home/subscribe-miot ; 程序的启动目录
command = php googlehome_subscribe.php ; 启动命令
numprocs = 1 ; 启动几个进程
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数,默认是 3
user = root ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
stdout_logfile = /www/wwwlogs/googlehome_subscribe.log
多进程启动
当 numprocs=1 时, process_name=%(program_name)s
当 numprocs>=2 时, process_name=%(program_name)s_%(process_num)s
[program:googlehome_subscribe]
directory = /www/wwwroot/google-home/subscribe-miot ; 程序的启动目录
command = php googlehome_subscribe.php ; 启动命令
process_name = %(program_name)s_%(process_num)s
numprocs = 2 ; 启动几个进程
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数,默认是 3
user = root ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
stdout_logfile = /www/wwwlogs/googlehome_subscribe.log
使用 supervisorctl 常用命令
supervisorctl 是 supervisord 的一个命令行客户端工具,可以通过它去执行一些命令。
# 查看进程状态 #
supervisorctl status
# 启动,停止,重启 所有进程 #
supervisorctl start all
supervisorctl stop all
supervisorctl restart all
# 启动某个进程(program_name=你配置中写的程序名称) #
supervisorctl start program_name
# 多进程重启 #
# 缺点:多个进程需要多次启动,这种比较平滑,可以写个 shell 去循环 restart #
supervisorctl restart program_name:program_name_0
supervisorctl restart program_name:program_name_1
# 这种通配符会同时重启所有进程,先 stop 所有,再 start 所有,不够平滑 #
supervisorctl restart program_name:*
# 更新配置,平滑启动 适用于:修改配置或新增配置 #
supervisorctl reread
supervisorctl update
# 重启所有进程 #
supervisorctl reload
报错整理
error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
# 解决方案 #
supervisord
supervisorctl reread
supervisorctl update
Check
配置文件改好之后 reload 就可以了,ps -aux | grep googlehome_subscribe
看一下有脚本启动就说明配置成功了。
更多用法参考文档:http://supervisord.org/
评论