Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。
也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
Debian / Ubuntu
可以直接通过apt安装:$ sudo apt-get install supervisor
apt-get
安装后,supervisor
的主配置文件在/etc/supervisor/supervisord.conf
/etc/supervisor/conf.d/*.conf
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
文件
1[Unit] 2Description=Supervisor process control system for UNIX 3Documentation=http://supervisord.org 4After=network.target 5 6[Service] 7ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf 8ExecStop=/usr/bin/supervisorctl 9ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf <span class="katex-html" aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;"><span class="strut" style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;"><span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">O<span class="mord mathit" style="margin-right:0.13889em;" style="font-size: inherit;color: inherit;line-height: inherit;">P<span class="mord mathit" style="margin-right:0.13889em;" style="font-size: inherit;color: inherit;line-height: inherit;">T<span class="mord mathit" style="margin-right:0.07847em;" style="font-size: inherit;color: inherit;line-height: inherit;">I<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">O<span class="mord mathit" style="margin-right:0.10903em;" style="font-size: inherit;color: inherit;line-height: inherit;">N<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">S<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">h<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02691em;" style="font-size: inherit;color: inherit;line-height: inherit;">w<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">n<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">x<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="margin-right:0.00773em;" style="font-size: inherit;color: inherit;line-height: inherit;">R<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;">l<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">a<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">b<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">n<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;">l<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">n<span class="mord mathit" style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;">fOPTIONS reload 10KillMode=process 11Restart=on-failure 12RestartSec=50s 13 14[Install] 15WantedBy=multi-user.target 16</span class="mord mathit" style="margin-right:0.10764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.00773em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02691em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.10903em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit" style="margin-right:0.07847em;"></span class="mord mathit" style="margin-right:0.13889em;"></span class="mord mathit" style="margin-right:0.13889em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="strut" style="height:1em;vertical-align:-0.25em;"></span class="katex-html" aria-hidden="true">用于向supervisord
服务发起命令。
supervisorctl -h
可以查看帮助说明
1sudo supervisorctl help 2 3default commands (type help <topic>): 4===================================== 5add exit open reload restart start tail 6avail fg pid remove shutdown status update 7clear maintail quit reread signal stop versionsudo systemctl restart supervisor.service
则会自动加载哦/home/www/python/echo_time.sh
为例
1#/bin/bash 2 3while true; 4do 5 echo date +%Y-%m-%d,%H:%m:%s 6 sleep 2 7done/etc/supervisor/conf.d/
新增子进程配置文件echo_time.conf
1; /etc/supervisor/conf.d/echo_time.conf 2 3[program:echo_time] 4 5command = /usr/bin/env sh /home/www/python/echo_time.sh 6directory = /home/www/python 7user = www 8startsecs = 3 9 10redirect_stderr = true 11stdout_logfile_maxbytes = 50MB 12stdout_logfile_backups = 10 13stdout_logfile = /home/www/python/log/echo_time.logecho_time
脚本就常驻运行起来了。可以通过日志查看运行情况:
1tail -f log/echo_time.log 22018-12-22,22:12:1545490695 32018-12-22,22:12:1545490697 42018-12-22,22:12:1545490699 52018-12-22,22:12:1545490701sudo supervisorctl status
查看子进程运行情况:
1sudo supervisorctl status 2echo_time RUNNING pid 28906, uptime 0:08:36supervisord.conf
注释掉的这4行 (/etc/supervisor/supervisord.conf
)
1[inet_http_server] ; inet (TCP) server disabled by default 2port=*:9001 ; (ip_address:port specifier, *:port for ;all iface) 3;username=www ; (default is no username (open server)) 4;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