Linux系统下 Supervisor 安装搭建
在 web 应用部署到线上后,需要保证应用一直处于运行状态,在遇到程序异常、报错等情况,导致 web 应用终止时,需要保证程序可以立刻重启,继续提供服务。
所以,就需要一个工具,时刻监控 web 应用的运行情况,管理该进程。
Supervisor 就是解决这种需求的工具,可以保证程序崩溃后,重新把程序启动起来等功能。
Supervisor 是一个用 Python 写的进程管理工具,可以很方便的用来在 UNIX-like 系统(不支持 Windows)下启动、重启(自动重启程序)、关闭进程(不仅仅是 Python 进程).
一、 安装supervisor
# yum install python-setuptools
# easy_install pip
# pip install supervisor -- 或者-- easy_install supervisor
# mkdir /etc/supervisor/
安装完成之后,在/etc/supervisor目录下生成配置文件
# echo_supervisord_conf>/etc/supervisor/supervisord.conf
卸载:
如果需要卸载supervisor
执行 # pip uninstall supervisor
或者
yum remove supervisord
修改supervisor配置文件: vi /etc/supervisor/supervisord.conf
把末尾的include去掉;添加配置文件
[include]
files = /etc/supervisor/conf.d/*.ini #(注意空格)
conf.d文件夹中的app_admin.ini配置文件如下
[root@centos]# vim appadmin.ini
[program:appadmin]
process_name=%(program_name)s_%(process_num)02d
command=php think queue:listen --queue CancelOrderJobQueue //执行一个自动关闭订单的队列
directory=/usr/share/nginx/html/dev/new_api ##程序的目录地址最好要添加,要不然会报错,找不到路径
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/etc/supervisor/logs/appadmin.log #日志文件 如果成功会看见.log文件
一切准备完毕后 执行
supervisord -c /etc/supervisor/supervisord.conf
启动supervisor,即可完成程序的持续运行!
运行 supervisorctl命令即可查看程序的运行状态
一些supervisor的常用命令
1 进入supervisor 命令--> #supervisorctl
2 查看命令 --> #help
3 重载项目 --> #reload
4 查看状态 --> #status
二、设置supervisor开机自起:
重启服务器后,发现supervisor开机启动,于是只好手动添加脚本 实现开机启动
Supervisor设置开机启动有很多,这里以Centos为例,仅供参考!
编辑sh文件并给予权限
vim /etc/init.d/supervisord
张贴:
#!/bin/bash
#
# supervisord This scripts turns supervisord on
#
# Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
# Jason Koppe <jkoppe@indeed.com> adjusted to read sysconfig,
# use supervisord tools to start/stop, conditionally wait
# for child processes to shutdown, and startup later
# Mikhail Mingalev <mingalevme@gmail.com> Merged
# redhat-init-jkoppe and redhat-sysconfig-jkoppe, and
# made the script "simple customizable".
# Brendan Maguire <maguire.brendan@gmail.com> Added OPTIONS to
# SUPERVISORCTL status call
#
# chkconfig: 345 83 04
#
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
# Script was originally written by Jason Koppe <jkoppe@indeed.com>.
#
. /etc/rc.d/init.d/functions
set -a
PREFIX=/usr
SUPERVISORD=$PREFIX/bin/supervisord
SUPERVISORCTL=$PREFIX/bin/supervisorctl
PIDFILE=/tmp/supervisord.pid
LOCKFILE=/var/lock/subsys/supervisord
OPTIONS="-c /etc/supervisor/supervisord.conf"
# unset this variable if you don't care to wait for child processes to shutdown before removing the $LOCKFILE-lock
WAIT_FOR_SUBPROCESSES=yes
# remove this if you manage number of open files in some other fashion
ulimit -n 96000
RETVAL=0
running_pid()
{
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
(cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
return 0
}
running()
{
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
# Obtain the pid and check it against the binary name
pid=`cat $PIDFILE`
running_pid $pid $SUPERVISORD || return 1
return 0
}
start() {
echo "Starting supervisord: "
if [ -e $PIDFILE ]; then
echo "ALREADY STARTED"
return 1
fi
# start supervisord with options from sysconfig (stuff like -c)
$SUPERVISORD $OPTIONS
# show initial startup status
$SUPERVISORCTL $OPTIONS status
# only create the subsyslock if we created the PIDFILE
[ -e $PIDFILE ] && touch $LOCKFILE
}
stop() {
echo -n "Stopping supervisord: "
$SUPERVISORCTL $OPTIONS shutdown
if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then
echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"
for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do
if [ ! -e $PIDFILE ] ; then
echo "Supervisord exited as expected in under $total_sleep seconds"
break
else
if [[ $sleep -eq "last" ]] ; then
echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"
return 1
else
sleep $sleep
total_sleep=$(( $total_sleep + $sleep ))
fi
fi
done
fi
# always remove the subsys. We might have waited a while, but just remove it at this point.
rm -f $LOCKFILE
}
restart() {
stop
start
}
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart|force-reload)
restart
RETVAL=$?
;;
reload)
$SUPERVISORCTL $OPTIONS reload
RETVAL=$?
;;
condrestart)
[ -f $LOCKFILE ] && restart
RETVAL=$?
;;
status)
$SUPERVISORCTL $OPTIONS status
if running ; then
RETVAL=0
else
RETVAL=1
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
保存后就可以添加至服务并设置开机启动啦。
chkconfig --add supervisord
chkconfig supervisord --level 345 on
启动、关闭、重启supervisord
命令
service supervisord start
service supervisord stop
service supervisord restart
参考地址:
https://segmentfault.com/a/1190000016012646