英文比较好的朋友,可以查看这篇文章
https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps
有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断。当进程中断的时候,希望能自动重新启动它。此时,我就需要使用到了Supervisor。
supervisor管理的进程必须由supervisor来启动
supervisor还要求管理的程序是非daemon程序,supervisord会帮你把它转成daemon程序,因此如果用supervisor来管理nginx的话,必须在nginx的配置文件里添加一行设置daemon off让nginx以非daemon方式启动。
supervisor工具包括4个组成部分:supervisord,supervisorctl,Web Server,XML-RPC Interface。
它负责在自己的调用启动子程序,响应客户端的命令,重新启动崩溃或退出的子进程,记录其子进程stdout和stderr输出
supervisord的命令行客户端,是一个shell界面,可以操作supervisord提供的操作功能。从supervisorctl,用户可以连接到不同的supervisord进程,获得每个子进程的状态,以及停止和启动子进程。
它通过UNIX socker或TCP socket和服务端进行连接。
在浏览器中执行supervisorctl的各种操作
和HTTP服务相似,这是提供了一个XML-RPC接口。你可以通过该接口去执行supervisorctl的操作。
其中最重要的是supervisord和supervisorctl。
sudo easy_install supervisor
【修改时间】:2017年7月3日
【增加内容】:现在的版本好像不需要再手动创建配置文件了。主配置文件默认为/etc/supervisor/supervisord.conf
,建议你把自己增加的配置文件放在/etc/supervisor/conf.d
目录下。主配置文件会包含该路径下的所有.conf
结尾的文件
sudo su - root -c "echo_supervisord_conf > /etc/supervisord.conf"
严谨的讲,你需要对这个文件进行权限设置,因为它可能包含未加密的用户名和密码。
在默认的配置文件中,supervisord.pid 以及 supervisor.sock 是放在 /tmp 目录下,但是 /tmp 目录是存放临时文件,里面的文件是会被 Linux 系统删除的,一旦这些文件丢失,就无法再通过 supervisorctl 来执行 restart 和 stop 命令了,将只会得到unix:///tmp/supervisor.sock不存在的错误 。
【修改时间】:2017年7月3日
【增加内容】:主配置文件默认为/etc/supervisor/supervisord.conf
,建议你把自己增加的配置文件放在/etc/supervisor/conf.d
目录下。主配置文件会包含该路径下的所有.conf
结尾的文件
sudo vi /etc/supervisord.conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Note: shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
[supervisord]
logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
;umask=022 ; (process file creation umask;default 022)
;user=chrism ; (default is current user, required if root)
;user=root ; (default is current user, required if root)
;password=rootpasswd ; (default is no password (open server))
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;autorestart=unexpected ; whether/when to restart (default: unexpected)
;startsecs=1 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
;[include]
;files = relative/directory/*.ini
在配置文件中,添加一个自己要执行的命令或自己写的脚本
vi /home/wang/testvisor.py
#! /use/bin/env python
from datetime import datetime
import time
while True:
print datetime.now()
time.sleep(7)
【修改时间】:2017年7月3日
【增加内容】:主配置文件默认为/etc/supervisor/supervisord.conf
,建议你把自己增加的配置文件放在/etc/supervisor/conf.d
目录下。主配置文件会包含该路径下的所有.conf
结尾的文件
vi /etc/supervisord.conf
在文件中添加下面的代码
[program:printdate]
command=python testvisor.py
directory=/home/wang
stdout_logfile=/var/log/supervisor/printdate.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
2017年3月20日更新
如果需要执行的命令行中有百分号(%),那么你需要用%%来代替。否则,%会认为是supervisor中的变量。
官方文档:
http://supervisord.org/configuration.html#program-x-section-settings
下面是我从网络上搜到的关于部分配置的中文解释
;[program:应用名称]
[program:cat]
;执行的命令
command=python printdate.py
;进程名称
;当numprocs为1时,process_name=%(program_name)s
;当numprocs>=2时,%(program_name)s_%(process_num)02d
process_name=%(program_name)s
;进程数量
numprocs=1
;执行命令的目录
directory=/home/wang
;掩码
umask=022
;优先级,值越高,启动的越晚,关闭的越早。默认999
priority=999
;是否自动启动。如果是true,当supervisor启动时,程序将会自动启动
autostart=true
autorestart=true
;值范围:false, unexpected, true
;false:不会自动重新启动该进程
;unexpected:当程序退出并且退出代码不是与此进程配置相关联的退出代码(请参阅exitcodes,默认值为0,2)时,该进程将重新启动
;true:进程将在退出时无条件地重新启动,不考虑其退出代码。
; 启动10秒后没有异常退出,就当作已经正常启动了。默认1秒
startsecs=10
;该命令退出后,尝试重新启动的次数,默认3次
startretries=3
;当退出码是多少时执行重启。默认值0,2
exitcodes=0,2
;停止信号
stopsignal=TERM
stopwaitsecs=10
;以哪个用户执行该命令
user=root
;错误重定向
redirect_stderr=false
;输出日志文件
stdout_logfile=/var/log/supervisor/printdate.log
;单个日志文件的最大容量
stdout_logfile_maxbytes=1MB
;最多几个日志文件
stdout_logfile_backups=10
;错误日志
stderr_logfile=/var/log/supervisor/printdateerr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
;环境变量设置
environment=A="1",B="2"
配置修改后,需要supervisorctl update后才可以生效
service supervisor start
service supervisor stop
service supervisor restart
或者
sudo supervisord -c /etc/supervisor/supervisord.conf
sudo supervisorctl shutdown
程序输出到日志文件
cat /var/log/supervisor/printdate.log
操作supervisord是通过supervisorctl
7.1:你可以进入supervisorctl的控制台
sudo supervisorctl
7.2:也可以在shell中直接执行控制命令
sudo supervisorctl status
7.3:控制命令
printdate为[program:printdate]里配置的值,这个示例就是printdate。
停止某一个进程
sudo supervisorctl stop printdate
启动某个进程
sudo supervisorctl start printdate
重启某个进程
sudo supervisorctl restart printdate
重启所有属于名为groupworker这个分组的进程(start,restart同理)
sudo supervisorctl stop groupworker:
停止全部进程。注:start、restart、stop都不会载入最新的配置文件。
sudo supervisorctl stop all
载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
sudo supervisorctl reload
根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
sudo supervisorctl update
关闭supervisord
sudo supervisorctl shutdown
注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
8.1:配置详情
https://github.com/Supervisor/supervisor/blob/master/docs/configuration.rst
8.2:supervisord的命令行参数
https://github.com/Supervisor/supervisor/blob/master/docs/running.rst#commandsupervisord-command-line-options
8.3:supervisorctl的命令行参数
https://github.com/Supervisor/supervisor/blob/master/docs/running.rst#commandsupervisorctl-command-line-options
8.4:ctl的控制命令
https://github.com/Supervisor/supervisor/blob/master/docs/running.rst#commandsupervisorctl-actions
8.5:其他
https://github.com/Supervisor/supervisor/tree/master/docs
http://blog.chinaunix.net/uid-26000296-id-4759916.html
http://www.kuqin.com/shuoit/20151008/348366.html
https://segmentfault.com/a/1190000000606682
http://liyangliang.me/posts/2015/06/using-supervisor/
http://www.tuicool.com/articles/Ejm2u2
http://www.tuicool.com/articles/vIRzQnR