Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。下面以CentOS7,Python2.7版本环境下,介绍Supervisor的安装与配置步聚:
1、安装Python包管理工具(easy_install)
<span style="color:#000000"><code>easy_install是setuptools包里带的一个命令,使用easy_install实际上是在调用setuptools来完成安装模块的工作,所以安装setuptools即可。
</code></span>
<span style="color:#000000"><code>wget --no-check-certificate https://bootstrap<span style="color:#009900 !important">.pypa</span><span style="color:#009900 !important">.io</span>/ez_setup<span style="color:#009900 !important">.py</span> -O - | sudo python</code></span>
2、安装supervisor
<span style="color:#000000"><code>easy_install supervisor</code></span>
supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令)、客户端(用于和守护进程通信,发送管理进程的指令)、生成初始配置文件程序。
3、配置
运行supervisord服务的时候,需要指定supervisor配置文件,如果没有显示指定,默认在以下目录查找:
<span style="color:#000000"><code>$CWD/supervisord<span style="color:#009900 !important">.conf</span>
$CWD/etc/supervisord<span style="color:#009900 !important">.conf</span>
/etc/supervisord<span style="color:#009900 !important">.conf</span>
/etc/supervisor/supervisord<span style="color:#009900 !important">.conf</span> (since Supervisor <span style="color:#006666 !important">3.3</span><span style="color:#006666 !important">.0</span>)
../etc/supervisord<span style="color:#009900 !important">.conf</span> (Relative to the executable)
../supervisord<span style="color:#009900 !important">.conf</span> (Relative to the executable)</code></span>
$CWD表示运行supervisord程序的目录。
可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:
<span style="color:#000000"><code>mkdir /etc/supervisor
<span style="color:#4f4f4f !important">echo</span>_supervisord_conf > /etc/supervisor/supervisord.conf</code></span>
4、配置文件参数说明
supervisor的配置参数较多,下面介绍一下常用的参数配置,详细的配置及说明,请参考官方文档介绍。
注:分号(;)开头的配置表示注释
<span style="color:#000000"><code><span style="color:#006666 !important">[unix_http_server]</span>
file=/tmp/supervisor.sock ;UNIX socket 文件,supervisorctl 会使用
<span style="color:#880000 !important"><em>;chmod=0700 ;socket文件的mode,默认是0700</em></span>
<span style="color:#880000 !important"><em>;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid</em></span>
<span style="color:#880000 !important"><em>;[inet_http_server] ;HTTP服务器,提供web管理界面</em></span>
<span style="color:#880000 !important"><em>;port=127.0.0.1:9001 ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性</em></span>
<span style="color:#880000 !important"><em>;username=user ;登录管理后台的用户名</em></span>
<span style="color:#880000 !important"><em>;password=123 ;登录管理后台的密码</em></span>
<span style="color:#006666 !important">[supervisord]</span>
logfile=/tmp/supervisord.log ;日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=<span style="color:#006666 !important">50</span>MB ;日志文件大小,超出会rotate,默认 <span style="color:#006666 !important">50</span>MB,如果设成<span style="color:#006666 !important">0</span>,表示不限制大小
logfile_backups=<span style="color:#006666 !important">10</span> ;日志文件保留备份数量默认<span style="color:#006666 !important">10</span>,设为<span style="color:#006666 !important">0</span>表示不备份
loglevel=info ;日志级别,默认info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=<span style="color:#000088 !important">false</span> ;是否在前台启动,默认是<span style="color:#000088 !important">false</span>,即以 daemon 的方式启动
minfds=<span style="color:#006666 !important">1024</span> ;可以打开的文件描述符的最小值,默认 <span style="color:#006666 !important">1024</span>
minprocs=<span style="color:#006666 !important">200</span> ;可以打开的进程数的最小值,默认 <span style="color:#006666 !important">200</span>
<span style="color:#006666 !important">[supervisorctl]</span>
serverurl=unix:///tmp/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
<span style="color:#880000 !important"><em>;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord</em></span>
<span style="color:#880000 !important"><em>; [program:xx]是被管理的进程配置参数,xx是进程的名称</em></span>
<span style="color:#006666 !important">[program:xx]</span>
command=/opt/apache-tomcat-<span style="color:#006666 !important">8.0</span>.<span style="color:#006666 !important">35</span>/bin/catalina.sh run ; 程序启动命令
autostart=<span style="color:#000088 !important">true</span> ; 在supervisord启动的时候也自动启动
startsecs=<span style="color:#006666 !important">10</span> ; 启动<span style="color:#006666 !important">10</span>秒后没有异常退出,就表示进程正常启动了,默认为<span style="color:#006666 !important">1</span>秒
autorestart=<span style="color:#000088 !important">true</span> ; 程序退出后自动重启,可选值:[unexpected,<span style="color:#000088 !important">true</span>,<span style="color:#000088 !important">false</span>],默认为unexpected,表示进程意外杀死后才重启
startretries=<span style="color:#006666 !important">3</span> ; 启动失败自动重试次数,默认是<span style="color:#006666 !important">3</span>
user=tomcat ; 用哪个用户启动进程,默认是root
priority=<span style="color:#006666 !important">999</span> ; 进程启动优先级,默认<span style="color:#006666 !important">999</span>,值小的优先启动
redirect_stderr=<span style="color:#000088 !important">true</span> ; 把stderr重定向到stdout,默认<span style="color:#000088 !important">false</span>
stdout_logfile_maxbytes=<span style="color:#006666 !important">20</span>MB ; stdout 日志文件大小,默认<span style="color:#006666 !important">50</span>MB
stdout_logfile_backups = <span style="color:#006666 !important">20</span> ; stdout 日志文件备份数,默认是<span style="color:#006666 !important">10</span>
<span style="color:#880000 !important"><em>; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)</em></span>
stdout_logfile=/opt/apache-tomcat-<span style="color:#006666 !important">8.0</span>.<span style="color:#006666 !important">35</span>/logs/catalina.out
stopasgroup=<span style="color:#000088 !important">false</span> ;默认为<span style="color:#000088 !important">false</span>,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=<span style="color:#000088 !important">false</span> ;默认为<span style="color:#000088 !important">false</span>,向进程组发送kill信号,包括子进程
<span style="color:#880000 !important"><em>;包含其它配置文件</em></span>
<span style="color:#006666 !important">[include]</span>
files = relative/directory/*.ini ;可以指定一个或多个以.ini结束的配置文件</code></span>
include示例:
<span style="color:#000000"><code>[<span style="color:#000088 !important">include</span>]
files = <span style="color:#008800 !important">/opt/absolute</span><span style="color:#008800 !important">/filename.ini /opt</span><span style="color:#008800 !important">/absolute/</span>*.ini foo.conf config??.ini</code></span>
- 1
- 2
5、配置管理进程
进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下包含进supervisord.conf文件中。
1> 创建/etc/supervisor/config.d目录,用于存放进程管理的配置文件
2> 修改/etc/supervisor/supervisord.conf中的include参数,将/etc/supervisor/conf.d目录添加到include中
<span style="color:#000000"><code>[<span style="color:#000088 !important">include</span>]
files = <span style="color:#008800 !important">/etc/supervisor</span><span style="color:#008800 !important">/config.d/</span>*.ini</code></span>
下面是配置Tomcat进程的一个例子:
<span style="color:#000000"><code><span style="color:#006666 !important">[program:tomcat]</span>
command=/opt/apache-tomcat-<span style="color:#006666 !important">8.0</span>.<span style="color:#006666 !important">35</span>/bin/catalina.sh run
stdout_logfile=/opt/apache-tomcat-<span style="color:#006666 !important">8.0</span>.<span style="color:#006666 !important">35</span>/logs/catalina.out
autostart=<span style="color:#000088 !important">true</span>
autorestart=<span style="color:#000088 !important">true</span>
startsecs=<span style="color:#006666 !important">5</span>
priority=<span style="color:#006666 !important">1</span>
stopasgroup=<span style="color:#000088 !important">true</span>
killasgroup=<span style="color:#000088 !important">true</span></code></span>
5、启动Supervisor服务
<span style="color:#000000"><code>supervisord -c /etc/supervisor/supervisord<span style="color:#009900 !important">.conf</span></code></span>
6、控制进程
6.1 交互终端
supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启。运行supervisorctl命令,不加参数,会进入supervisor客户端的交互终端,并会列出当前所管理的所有进程。
<span style="color:#000000"><code>上图中的tomcat就是我们在配置文件中[program:tomcat]指定的名字。
</code></span>
输入help可以查看可以执行的命令列表,如果想看某个命令的作用,运行help 命令名称,如:help stop
<span style="color:#000000"><code><span style="color:#000088 !important">stop</span> tomcat // 表示停止tomcat进程
<span style="color:#000088 !important">stop</span> all // 表示停止所有进程
// <span style="color:#000088 !important">...</span></code></span>
6.2 bash终端
<span style="color:#000000"><code>supervisorctl status
supervisorctl stop tomcat
supervisorctl <span style="color:#000088 !important">start</span> tomcat
supervisorctl restart tomcat
supervisorctl reread
supervisorctl <span style="color:#000088 !important">update</span></code></span>
6.3 Web管理界面
出于安全考虑,默认配置是没有开启web管理界面,需要修改supervisord.conf配置文件打开http访权限,将下面的配置:
<span style="color:#000000"><code>;[inet_http_server] ; inet (TCP) server disabled by <span style="color:#000088 !important">default</span>
;<span style="color:#000088 !important">port</span>=<span style="color:#006666 !important">127.0</span><span style="color:#006666 !important">.0</span><span style="color:#006666 !important">.1</span>:<span style="color:#006666 !important">9001</span> ; (ip_address:<span style="color:#000088 !important">port</span> specifier, *:<span style="color:#000088 !important">port</span> <span style="color:#000088 !important">for</span> <span style="color:#000088 !important">all</span> iface)
;username=user ; (<span style="color:#000088 !important">default</span> <span style="color:#000088 !important">is</span> no username (<span style="color:#000088 !important">open</span> server))
;password=<span style="color:#006666 !important">123</span> ; (<span style="color:#000088 !important">default</span> <span style="color:#000088 !important">is</span> no password (<span style="color:#000088 !important">open</span> server))</code></span>
修改成:
<span style="color:#000000"><code>[inet_http_server] ; inet (TCP) server disabled by <span style="color:#000088 !important">default</span>
<span style="color:#000088 !important">port</span>=<span style="color:#006666 !important">0.0</span><span style="color:#006666 !important">.0</span><span style="color:#006666 !important">.0</span>:<span style="color:#006666 !important">9001</span> ; (ip_address:<span style="color:#000088 !important">port</span> specifier, *:<span style="color:#000088 !important">port</span> <span style="color:#000088 !important">for</span> <span style="color:#000088 !important">all</span> iface)
username=user ; (<span style="color:#000088 !important">default</span> <span style="color:#000088 !important">is</span> no username (<span style="color:#000088 !important">open</span> server))
password=<span style="color:#006666 !important">123</span> ; (<span style="color:#000088 !important">default</span> <span style="color:#000088 !important">is</span> no password (<span style="color:#000088 !important">open</span> server))</code></span>
port:绑定访问IP和端口,这里是绑定的是本地IP和9001端口
username:登录管理后台的用户名
password:登录管理后台的密码
7、开机启动Supervisor服务
7.1 配置systemctl服务
1> 进入/lib/systemd/system目录,并创建supervisor.service文件
<span style="color:#000000"><code><span style="color:#006666 !important">[Unit]</span>
Description=supervisor
After=network.target
<span style="color:#006666 !important">[Service]</span>
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=<span style="color:#000088 !important">on</span>-failure
RestartSec=<span style="color:#006666 !important">42</span>s
<span style="color:#006666 !important">[Install]</span>
WantedBy=multi-user.target</code></span>
2> 设置开机启动
<span style="color:#000000"><code>systemctl enable supervisor<span style="color:#009900 !important">.service</span>
systemctl daemon-reload</code></span>
3、修改文件权限为766
<span style="color:#000000"><code><span style="color:#000088 !important">chmod</span> <span style="color:#006666 !important">766</span> supervisor.service</code></span>
7.2 配置service类型服务
<span style="color:#000000"><code><span style="color:#006666 !important">#!/bin/bash</span>
<span style="color:#880000 !important"><em>#</em></span>
<span style="color:#880000 !important"><em># supervisord This scripts turns supervisord on</em></span>
<span style="color:#880000 !important"><em>#</em></span>
<span style="color:#880000 !important"><em># Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)</em></span>
<span style="color:#880000 !important"><em>#</em></span>
<span style="color:#880000 !important"><em># chkconfig: - 95 04</em></span>
<span style="color:#880000 !important"><em>#</em></span>
<span style="color:#880000 !important"><em># description: supervisor is a process control utility. It has a web based</em></span>
<span style="color:#880000 !important"><em># xmlrpc interface as well as a few other nifty features.</em></span>
<span style="color:#880000 !important"><em># processname: supervisord</em></span>
<span style="color:#880000 !important"><em># config: /etc/supervisor/supervisord.conf</em></span>
<span style="color:#880000 !important"><em># pidfile: /var/run/supervisord.pid</em></span>
<span style="color:#880000 !important"><em>#</em></span>
<span style="color:#880000 !important"><em># source function library</em></span>
. /etc/rc.d/init.d/functions
RETVAL=<span style="color:#006666 !important">0</span>
<span style="color:#009900 !important">start</span>() {
<span style="color:#4f4f4f !important">echo</span> -n $<span style="color:#009900 !important">"Starting supervisord: "</span>
daemon <span style="color:#009900 !important">"supervisord -c /etc/supervisor/supervisord.conf "</span>
RETVAL=$?
<span style="color:#4f4f4f !important">echo</span>
[ <span style="color:#009900 !important">$RETVAL</span> -eq <span style="color:#006666 !important">0</span> ] && touch /var/lock/subsys/supervisord
}
<span style="color:#009900 !important">stop</span>() {
<span style="color:#4f4f4f !important">echo</span> -n $<span style="color:#009900 !important">"Stopping supervisord: "</span>
killproc supervisord
<span style="color:#4f4f4f !important">echo</span>
[ <span style="color:#009900 !important">$RETVAL</span> -eq <span style="color:#006666 !important">0</span> ] && rm -f /var/lock/subsys/supervisord
}
<span style="color:#009900 !important">restart</span>() {
stop
start
}
<span style="color:#000088 !important">case</span> <span style="color:#009900 !important">"<span style="color:#009900 !important">$1</span>"</span> <span style="color:#000088 !important">in</span>
start)
start
;;
stop)
stop
;;
restart|force-reload|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/supervisord ] && restart
;;
status)
status supervisord
RETVAL=$?
;;
*)
<span style="color:#4f4f4f !important">echo</span> $<span style="color:#009900 !important">"Usage: <span style="color:#009900 !important">$0</span> {start|stop|status|restart|reload|force-reload|condrestart}"</span>
<span style="color:#000088 !important">exit</span> <span style="color:#006666 !important">1</span>
<span style="color:#000088 !important">esac</span>
<span style="color:#000088 !important">exit</span> <span style="color:#009900 !important">$RETVAL</span>
</code></span>
将上述脚本内容保存到/etc/rc.d/init.d/supervisor文件中,修改文件权限为755,并设置开机启动
<span style="color:#000000"><code>chmod <span style="color:#006666 !important">755</span> /etc/rc.d/init.d/supervisor
chkconfig supervisor <span style="color:#000088 !important">on</span></code></span>
注意:修改脚本中supervisor配置文件路径为你的supervisor的配置文件路径
其它Linux发行版开机启动脚本:https://github.com/Supervisor/initscripts
注意:
Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程。否则提示Exited too quickly (process log may have details)异常。例子中的Tomcat默认是以守护进程启动的,所以我们改成了catalina.sh run,以前台进程的方式运行。
yum方式安装
<span style="color:#000000"><code>yum <span style="color:#000088 !important">install</span> epel-release
yum <span style="color:#000088 !important">install</span> -y supervisor</code></span>
supervisor没有发布在标准的CentOS源在,需要安装epel源。这种方式安装的可能不是最新版本,但比较方便,安装完成之后,配置文件会自动帮你生成。
默认配置文件:/etc/supervisord.conf
进程管理配置文件放到:/etc/supervisord.d/目录下即可
默认日志文件:/tmp/supervisord.log,可以查看进程的启动信息