Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本

1、 安装

源码安装

下载和解压 https://pypi.org/project/supervisor/#files

cd supervisor-3.3.3
python setup.py install

pip安装

http://supervisord.org/installing.html

easy_install supervisor
或者
pip install supervisor

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)

可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

配置文件参数说明

http://supervisord.org/configuration.html

supervisor的配置参数较多,下面介绍一下常用的参数配置,详细的配置及说明,请参考官方文档介绍。

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
logfile_maxbytes=500MB
logfile_backups=10 
loglevel=info                ; (log level;default info; others: debug,warn,trace)
nodaemon=false               ; (start in foreground if true;default false)
minfds=655350                ; (min. avail startup file descriptors;default 1024)
minprocs=65535                 ; (min. avail process descriptors;default 200)

[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

[include]
files = /etc/supervisord.conf.d/*.conf

子配置文件说明

[program:logstash-prod-access]
directory = /data/software/logstash-5.5.2
command = /data/software/logstash-5.5.2/bin/logstash --path.data=/var/lib/logstash -f /etc/logstash/conf.d/indexer-prod-access.conf -l /data/logs/logstash/logstash-prod-access/
autostart = true
startsecs = 10
autorestart = true
redirect_stderr=false
startretries = 3
user = root
numprocs_start=1
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /data/logs/supervisord/logstash-indexer-prod-access.log
stderr_logfile_maxbytes = 500MB
stderr_logfile_backups = 10
stderr_logfile = /data/logs/supervisord/logstash-indexer-prod-access-err.log

命令

supervisord -c /etc/supervisor/supervisord.conf # 启动
supervisorctl status
supervisorctl stop app
supervisorctl start app
supervisorctl restart app
supervisorctl reread 
supervisorctl update  #更新
supervisorctl shutdown #停止

开机启动Supervisor服务

进入/lib/systemd/system目录,并创建supervisor.service文件

# supervisord service for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord
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

修改文件权限为766

chmod 766 supervisor.service

其它Linux发行版开机启动脚本:https://github.com/Supervisor/initscripts

注意

Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程。否则提示Exited too quickly (process log may have details)异常。例子中的Tomcat默认是以守护进程启动的,所以tomcat必须改成了catalina.sh run,以前台进程的方式运行。

参考:https://blog.csdn.net/xyang81/article/details/51555473