一、supervisor简介
supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。
二、安装方法
2.1 yum 安装(版本老旧,不推荐)
yum install -y supervisor
2.2 pip 安装
Python3 环境安装:
pip3 install git+https://github.com/Supervisor/supervisor
Python2 环境安装:
pip install supvervisor
生成默认配置文件
echo_supervisord_conf > /etc/supervisord.conf
mkdir /etc/supervisor
添加下面两行到 /etc/supervisord.conf 末尾
[include]
files = /etc/supervisor/*.ini
2.3 解决pip 安装没有开机启动服务
vi /etc/init.d/supervisord
开机启动脚本内容见2.5节!!!
2.4 服务器启动报错
启动服务: service start supervisord
,如果启动失败则添加符号链接:
ln -s /usr/local/bin/supervisord /usr/bin/supervisord
2.5 开机启动脚本
#!/bin/bash
#
# supervisord This scripts turns supervisord on
#
# Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
#
# chkconfig: - 95 04
#
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
# processname: supervisord
# config: /etc/supervisord.conf
# pidfile: /var/run/supervisord.pid
#
# source function library
. /etc/rc.d/init.d/functions
RETVAL=0
start() {
echo -n $"Starting supervisord: "
daemon supervisord
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}
stop() {
echo -n $"Stopping supervisord: "
killproc supervisord
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/supervisord ] && restart
;;
status)
status supervisord
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
2.6 program配置参考
1、编写自己程序的拉起脚本,并放到/etc/supervisor目录下。
2、添加新脚本需要restart或reload 服务。
service restart supervisord 或
service reload supervisord
3、下面是用supervisor 管理kafka进程的示例:
[program:kafka_zk]
process_name=%(program_name)s
directory=/opt/kafka/kafka
command=/opt/kafka/kafka/bin/zookeeper-server-start.sh config/zookeeper.properties
numprocs=1
stopsignal=TERM
stopwaitsecs=3
stopasgroup=false
killasgroup=false
user=root
stdout_logfile=/var/tmp/%(program_name)s_inf.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=1
stdout_capture_maxbytes=1MB
stderr_logfile=/var/tmp/%(program_name)s_err.log
stderr_logfile_maxbytes=20MB
stderr_logfile_backups=1
stderr_capture_maxbytes=1MB