一、supervisor概述
supervisor是一个c/s系统,被用来在类Unix系统中监控进程状态。supervisor使用python开发。 服务端进程为supervisord,主要负责启动自身及其监控的子进程,响应客户端命令,重启异常退出的子进程,记录子进程stdout和stderr输出,生成和处理子进程生命周期中的事件。其配置文件一般为/etc/supervisord.conf,可以在配置文件中配置相关参数,包括supervisord自身的状态,其管理的各个子进程的相关属性等。supervisor的客户端为supervisorctl,它提供了一个类shell的接口(即命令行)来操作supervisord服务端。通过supervisorctl,可以连接到supervisord服务进程,获得服务进程监控的子进程状态,启动和停止子进程,获得正在运行的进程列表。客户端通过Unix域套接字或者TCP套接字与服务进程进行通信,服务器端具有身份凭证认证机制,可以有效提升安全性。当客户端和服务端位于同一台机器上时,客户端与服务器共用同一个配置文件/etc/supervisord.conf,通过不同标签来区分两者的配置。supervisor也提供了一个web页面来查看和管理进程状态。
二、supervisor安装及相关配置
(1)安装
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz#md5=04766d62864da13d6a12f7429e75314f
tar zxvf supervisor-3.3.2.tar.gz && cd supervisor-3.3.2
python setup.py install
supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl以及echo_supervisord_conf,它们分别是supervisor的守护进程服务(用于接收进程管理命令)、
客户端(用于和守护进程通信,发送管理进程的指令)以及生成初始配置文件程序。
(2)配置
运行supervisord服务的时候,需要指定supervisor配置文件,如果没有显示指定,默认在以下目录或文件中查找(其中$CWD表示运行supervisord程序的目录):
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
../etc/supervisord.conf (Relative to the executable)
../supervisord.conf (Relative to the executable)
安装完成后,可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:
echo_supervisord_conf > /etc/supervisord.conf 生成supervisor的主配置文件
mkdir /etc/supervisord.d 用户存放被监控进程的配置文件
(3)配置文件参数说明
supervisor的配置参数较多,详细的配置参数说明请参考官方文档介绍,下面介绍一些常用的参数配置,分号(;)开头的配置表示注释。
[unix_http_server] ;[inet_http_server] ; 侦听在TCP上的socket,Web Server和远程的supervisorctl都要用到它,如果不设置,默认为不开启。非必须设置项 [supervisord] ;主要定义服务端进程supervisord的相关属性。必须设置项 ; the below section must remain in the config file for RPC [rpcinterface:supervisor] ;该参数为XML_RPC服务,如果使用supervisord或者web server,该选项必须要开启 [supervisorctl] ;主要针对supervisorctl的一些属性配置 ; The below sample program section shows all possible program subsection values, ;[program:theprogramname] ; 管理的子进程,":"后面是子进程名字,最好和实际进程相关联。program可以设置一个或多个,一个program就是一个要被管理的进程 ; The below sample eventlistener section shows all possible ;[eventlistener:theeventlistenername] ;与program功能类似,也是suopervisor启动的子进程,不过它是订阅supervisord发送的event。它的名字就叫 ; The below sample group section shows all possible group values, ;[group:thegroupname] ; 给programs分组,划分到组里面的program。设置后就不用一个一个去操作了我们可以对组名进行统一的操作。 ; The [include] section can just contain the "files" setting. This ;[include] ; 有用的配置项,当管理的进程很多时,写一个配置文件就会很多,不够清晰。 include示例: |
三、配置管理进程
进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下,并包含进supervisord.conf文件中。
创建/etc/supervisord.d目录,用于存放进程管理的配置文件
修改/etc/supervisord.conf中的include参数,将/etc/supervisor.d目录添加到include中,实例如下
; Sample supervisor config file. ; ; For more information on the config file, please see: ; http://supervisord.org/configuration.html ; ; Notes: ; - Shell expansion ("~" or "$HOME") is not supported. Environment ; variables can be expanded using this syntax: "%(ENV_HOME)s". ; - Quotes around values are not supported, except in the case of ; the environment= options as shown below. ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". ; - Command will be truncated if it looks like a config file comment, e.g. ; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ". [unix_http_server] file=/var/run/supervisor.sock ; the path to the socket file #chmod=0777 ; 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=*: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 ; # of main logfile backups; 0 means none, 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 ;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 rpcinterface:supervisor 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:x] sections. [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface ; The supervisorctl section configures how supervisorctl will connect to ; supervisord. configure it match the settings in either the unix_http_server ; or inet_http_server section. [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 in [*_http_server] if set ;password=123 ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available ; The sample program section below 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) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (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 (0 means none, 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 (0 means none, 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 sample eventlistener section below shows all possible eventlistener ; subsection values. Create one or more 'real' eventlistener: sections to be ; able to handle event notifications sent by supervisord. ;[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) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; autorestart if exited after running (def: unexpected) ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (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=false ; redirect_stderr=true is not allowed for eventlisteners ;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 (0 means none, 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=10 ; # of stderr logfile backups (0 means none, 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 sample group section below 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 = /etc/supervisord.d/*.conf
#为了方便管理,增加一个tornado组 [group:tornados] programs=tornado-0,tornado-1 # 分别定义两个tornado的进程配置 [program:tornado-0] # 进程要执行的命令 #command=python /home/mcp/tornado/hello.py --port=8000 command=python /home/mcp/tornado/hello.py 8000 directory=/home/mcp/tornado/ user=mcp autostart=true # 自动重启 autorestart=true redirect_stderr=true # 日志路径 stdout_logfile=/home/mcp/tornado/tornado0.log loglevel=info [program:tornado-1] #command=python /home/mcp/tornado/hello.py --port=8001 command=python /home/mcp/tornado/hello.py 8001 directory=/home/mcp/tornado/ user=mcp autostart=true autorestart=true redirect_stderr=true stdout_logfile=/home/mcp/tornado/tornado1.log loglevel=info
四、supervisor相关命令
supervisord相关命令:
supervisord 启动服务端进程
/usr/bin/supervisord -c /etc/supervisord.conf 按指定配置文件启动服务端进程
supervisorctl相关命令:
supervisorctl 进入交互界面
supervisorctl status 查看被监控进程状态
supervisorctl stop all 关闭被监控的进程
supervisorctl start all 启动被监控的进程
supervisorctl start program-name 其中program-name为配置文件[program:xx]中的xx
supervisorctl stop program-name 其中program-name为配置文件[program:xx]中的xx
supervisorctl restart all 重启被监控的进程
supervisorctl reatart program-name 重启某一进程,program-name为[program:xx]中的xx
supervisorctl shutdown 关闭supervisord服务端
supervisorctl reload 重新加载配置文件
五、把supervisor加入开机自启动服务(CentOS7.X系统)
(1)利用/etc/rc.local
echo "/usr/bin/supervisord -c /etc/supervisord.conf" >> /etc/rc.local
/etc/rc.local -> rc.d/rc.local /etc/rc.local是/etc/rc.d/rc.local的软连接
如果开机启动不生效,则首先需要检查下/etc/rc.d/rc.local是否具有可执行权限
(2)加入systemctl管理
vim /lib/systemd/system/supervisor.service
[Unit]
Description=supervisor
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
上述文件编写后,执行如下命令即可:
systemctl enable supervisor.service 加入开机自启动服务
systemctl daemon-reload 重新载入systemd,扫描新的或有变动的单元(必要步骤)
chmod 766 /lib/systemd/system/supervisor.service 修改文件权限
六、把supervisor加入systemctl管理
通过上述(五),实际上supervisor已经加入了systemctl管理了,后续起停supervisor服务都可以通过systemctl来控制了
systemctl start supervisor.service 启动服务
systemctl stop supervisor.service 停止服务
systemctl restart supervisor.service 重新启动服务
systemctl reload supervisor.service 重载配置文件
systemctl status supervisor.service 查看服务状态(显示的类似于操作记录)