supervisor 是一款进程守护的软件,具备极其强大的功能。一般来说,Linux对程序的自动启动有着非常的严格的限制。实际上有时我们需要开机启动一条命令,这样的操作在Linux上直接设置是很困难的。好在Ubuntu 16.04目前采用了systemctl进行程序自启控制,但是使用起来仍然比较繁琐。因此supervisor可以说是每台Linux上必备的软件。目前我只用到它的两个功能
- 1、管理程序自启
- 2、程序挂掉之后自动启动
安装
安装直接用pip安装即可
pip install supervisor
supervisor需要有个配置文件,此文件可用supervisor官方命令生成,生成此文件需要有root权限
sudo echo_supervisord_conf > /etc/supervisord.conf
上述的/etc/supervisord.conf是在etc目录下生成配置文件,配置文件默认的名字就是supervisord.conf,一般不要修改成其他的名字。对于配置文件的位置,官方说是有默认的位置的Configuration File
如果直接运行
supervisord
supervisord就会依次从上述目录下寻找supervisord.conf。
参考Installing and Configuring Supervisor on Ubuntu 16.04,里面的教程
echo_supervisord_conf > /etc/supervisor/supervisord.conf #在/etc/supervisor/目录下生成配置文件
mkdir /etc/supervisor/conf.d # 在/etc/supervisor/下创建conf.d的文件夹,用于存放各种小的配置文件
此时打开supervisord.conf,翻到最后,将最后两行取消注释并修改为如下
[include]
files=conf.d/*.conf
supervisor支持include各种各样的配置文件,就在conf.d文件夹内,只要是.conf后缀都会被程序识别。
这样,supervisor的启动步骤就是
supervisord -c /etc/supervisor/supervisord.conf
配置程序
supervisor的使用可以参考yang xiang的博客
总结来说,就在我们创建的conf.d文件夹中设置我们需要启动的命令即可。
譬如我们创建了一个frp.conf的文件,内容如下
[program:my_frps]
command = /root/application/frp/frps -c /root/application/frp/frps_full.ini
autostart = true
autorestart = true
user = root
其中my_frps是program的名字,command是启动frps的具体命令,就和在shell中敲的一样,user即为指定的运行用户。至于我们的文件名,frp.conf是没有什么意义的,只要他是.conf为后缀即可。
supervisord 和 supervisorctl辨析
supervisor一共有两个(实际上好几个)主要的函数,supervisord 和 supervisorctl。前者就类似服务端,用于启动supervisor服务,后者就像客户端,用于控制某些程序的start、stop 和 restart.
supervisorctl my_frps start #启动frps
supervisorctl my_frps stop #关闭frps
更详细的操作可以参考supervisor(一)基础篇
设置supervisor开机自启
supervisor是控制其他程序启动的,可是他自己的启动应该如何操作呢。对于Ubuntu16.04和centos 7引入了systemctl,大大简化了自启程序的设置。
我们只需要把supervisor设置为系统服务,放到/etc/systemd/system/目录下即可。教程仍然参考Installing and Configuring Supervisor on Ubuntu 16.04
vim /etc/systemd/system/supervisord.service
[Unit]
Description=Supervisor daemon
Documentation=http://supervisord.org
After=network.target
[Service]
ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisor/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/local/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
注意原版的教程有句话Alias=supervisord.service,这句话请不要加,否则会报错。将supervisord.service加入开机自启中。
systemctl enable supervisord.service
此时重启,就会发现,supervisor已经启动了。
在centos 6.5 上自动启动
设置开机自启,参考教程
vim /etc/init.d/supervisord
#!/bin/bash
#
# supervisord This scripts turns supervisord on
# 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.
#
# source function library
. /etc/rc.d/init.d/functions
set -a
PREFIX=/usr
SUPERVISORD=$PREFIX/bin/supervisord
SUPERVISORCTL=$PREFIX/bin/supervisorctl
PIDFILE=/var/supervisor/supervisord.pid
LOCKFILE=/var/supervisor/supervisord.lock
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
需要修改的几处
PREFIX=/usr
SUPERVISORD=$PREFIX/bin/supervisord ##supervisord 程序的安装路径
SUPERVISORCTL=$PREFIX/bin/supervisorctl ##supervisorctl 程序的安装路径
PIDFILE=/var/supervisor/supervisord.pid ##需要先创建/var/supervisor目录
LOCKFILE=/var/supervisor/supervisord.lock
OPTIONS="-c /etc/supervisor/supervisord.conf" ##配置文件的路径
保存完毕之后,可以执行以下命令修改文件权限:
chmod 777 /etc/init.d/supervisord
/etc/init.d/supervisord start
或
service supervisord start
到这一步,一定要测试一下新建的supervisord的命令是否正常运行且不报错,如果报错,需要进行针对性的修改。
配置自动开机命令
chkconfig supervisord on
chkconfig --list | grep supervisord
supervisor的使用可以参考yang xiang的博客
假设想守护frps进程,并令其开机启动。在/etc/supervisord.conf最后加入下面的话。
[program:frps]
command = /root/Downloads/frp_0.10.0_linux_amd64/frps -c /root/Downloads/frp_0.10.0_linux_amd64/frps.ini
autostart = true
autorestart = true
user = root
其中command是启动frps的命令。
service supervisord restart
supervisorctl reload
supervisorctl start frpc
supervisorctl stop frpc
在Ubuntu14.04上设置
先附两个教程
Ubuntu 14.04下进程管理工具supervisor安装
supervisor安装部署文档和管理实例
网友评论