简述
Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。
也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
安装
Debian / Ubuntu
可以直接通过apt安装:$ sudo apt-get install supervisor
配置文件
目录结构
supervisor├── conf.d│ └── echo_time.conf -- 业务配置文件└── supervisord.conf -- 主配置文件,一般不需要改动
- 使用
apt-get
安装后,supervisor
的主配置文件在/etc/supervisor/supervisord.conf
子进程配置文件在
/etc/supervisor/conf.d/*.conf
supervisord 和 supervisorctl的关系
supervisord 是主进程
常用命令
# 使用默认的配置文件 /etc/supervisord.confsupervisord# 明确指定配置文件supervisord -c /etc/supervisord.conf# 使用 user 用户启动 supervisordsupervisord -u user
服务命令
- 查看服务状态:
sudo systemctl status supervisor.service
- 开启服务:
sudo systemctl start supervisor.service
- 停止服务:
sudo systemctl stop supervisor.service
- 重启服务:
sudo systemctl restart supervisor.service
- 开机启动项:
sudo systemctl enable supervisor.service
- 查看服务状态:
supervisor.service
文件[Unit]Description=Supervisor process control system for UNIXDocumentation=http://supervisord.orgAfter=network.target[Service]ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.confExecStop=/usr/bin/supervisorctl $OPTIONS shutdownExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reloadKillMode=processRestart=on-failureRestartSec=50s[Install]WantedBy=multi-user.target
supervisorctl 是客户端程序
用于向supervisord
服务发起命令。所有命令,通过
supervisorctl -h
可以查看帮助说明sudo supervisorctl helpdefault commands (type help
):=====================================add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version 控制子进程命令
# 停止某一个进程,program_name 为 [program:x] 里的 xsupervisorctl stop program_name# 启动某个进程supervisorctl start program_name# 重启某个进程supervisorctl restart program_name# 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)supervisorctl stop groupworker:# 结束 groupworker:name1 这个进程 (start,restart 同理)supervisorctl stop groupworker:name1# 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件supervisorctl stop all# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程supervisorctl reload# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启supervisorctl update
修改进程配置文件后,只要
sudo systemctl restart supervisor.service
则会自动加载哦
示例
以简单的
/home/www/python/echo_time.sh
为例#/bin/bashwhile true;do echo `date +%Y-%m-%d,%H:%m:%s` sleep 2done
在
/etc/supervisor/conf.d/
新增子进程配置文件echo_time.conf
; /etc/supervisor/conf.d/echo_time.conf[program:echo_time]command = /usr/bin/env sh /home/www/python/echo_time.shdirectory = /home/www/pythonuser = wwwstartsecs = 3redirect_stderr = truestdout_logfile_maxbytes = 50MBstdout_logfile_backups = 10stdout_logfile = /home/www/python/log/echo_time.log
然后启动程序:
$ supervisorctl reread$ supervisorctl update
这两个命令分别代表重新读取配置、更新子进程组
这样刚才添加的
echo_time
脚本就常驻运行起来了。可以通过日志查看运行情况:tail -f log/echo_time.log 2018-12-22,22:12:15454906952018-12-22,22:12:15454906972018-12-22,22:12:15454906992018-12-22,22:12:1545490701
也可以使用
sudo supervisorctl status
查看子进程运行情况:sudo supervisorctl statusecho_time RUNNING pid 28906, uptime 0:08:36
web界面操作
需要开启主配置文件
supervisord.conf
注释掉的这4行 (/etc/supervisor/supervisord.conf
)[inet_http_server] ; inet (TCP) server disabled by defaultport=*:9001 ; (ip_address:port specifier, *:port for ;all iface);username=www ; (default is no username (open server));password=www123456 ; (default is no password (open server))
注意:如果修改配置文件时, [inet_http_server]这一行被注释,会导致不仅web需要认证,命令行使用 supervisorctl也需要认证,这时候就需要在交互式命令行里输入用户名、密码才能进行下一步的操作。
浏览器访问:
http://myip:9001
,输入用户名、密码后,即可看到web页面:
遇到的错误
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord