supervisor 是一款進(jìn)程守護(hù)的軟件往声,具備極其強(qiáng)大的功能。一般來說戳吝,Linux對程序的自動啟動有著非常的嚴(yán)格的限制浩销。實際上有時我們需要開機(jī)啟動一條命令,這樣的操作在Linux上直接設(shè)置是很困難的听哭。好在Ubuntu 16.04目前采用了systemctl進(jìn)行程序自啟控制慢洋,但是使用起來仍然比較繁瑣。因此supervisor可以說是每臺Linux上必備的軟件陆盘。目前我只用到它的兩個功能
- 1普筹、管理程序自啟
- 2、程序掛掉之后自動啟動
安裝
安裝直接用pip安裝即可
pip install supervisor
supervisor需要有個配置文件隘马,此文件可用supervisor官方命令生成太防,生成此文件需要有root權(quán)限
sudo echo_supervisord_conf > /etc/supervisord.conf
上述的/etc/supervisord.conf是在etc目錄下生成配置文件,配置文件默認(rèn)的名字就是supervisord.conf酸员,一般不要修改成其他的名字蜒车。對于配置文件的位置讳嘱,官方說是有默認(rèn)的位置的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/下創(chuàng)建conf.d的文件夾沥潭,用于存放各種小的配置文件
此時打開supervisord.conf,翻到最后嬉挡,將最后兩行取消注釋并修改為如下
[include]
files=conf.d/*.conf
supervisor支持include各種各樣的配置文件钝鸽,就在conf.d文件夾內(nèi),只要是.conf后綴都會被程序識別棘伴。
這樣寞埠,supervisor的啟動步驟就是
supervisord -c /etc/supervisor/supervisord.conf
配置程序
supervisor的使用可以參考yang xiang的博客
總結(jié)來說屁置,就在我們創(chuàng)建的conf.d文件夾中設(shè)置我們需要啟動的命令即可焊夸。
譬如我們創(chuàng)建了一個frp.conf的文件,內(nèi)容如下
[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一共有兩個(實際上好幾個)主要的函數(shù)鲁僚,supervisord 和 supervisorctl。前者就類似服務(wù)端裁厅,用于啟動supervisor服務(wù)冰沙,后者就像客戶端,用于控制某些程序的start执虹、stop 和 restart.
supervisorctl my_frps start #啟動frps
supervisorctl my_frps stop #關(guān)閉frps
更詳細(xì)的操作可以參考supervisor(一)基礎(chǔ)篇
設(shè)置supervisor開機(jī)自啟
supervisor是控制其他程序啟動的拓挥,可是他自己的啟動應(yīng)該如何操作呢。對于Ubuntu16.04和centos 7引入了systemctl袋励,大大簡化了自啟程序的設(shè)置侥啤。
我們只需要把supervisor設(shè)置為系統(tǒng)服務(wù),放到/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加入開機(jī)自啟中糠雨。
systemctl enable supervisord.service
此時重啟,就會發(fā)現(xiàn)徘跪,supervisor已經(jīng)啟動了甘邀。
在centos 6.5 上自動啟動
設(shè)置開機(jī)自啟琅攘,參考教程
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 ##需要先創(chuàng)建/var/supervisor目錄
LOCKFILE=/var/supervisor/supervisord.lock
OPTIONS="-c /etc/supervisor/supervisord.conf" ##配置文件的路徑
保存完畢之后,可以執(zhí)行以下命令修改文件權(quán)限:
chmod 777 /etc/init.d/supervisord
/etc/init.d/supervisord start
或
service supervisord start
到這一步松邪,一定要測試一下新建的supervisord的命令是否正常運行且不報錯坞琴,如果報錯,需要進(jìn)行針對性的修改逗抑。
配置自動開機(jī)命令
chkconfig supervisord on
chkconfig --list | grep supervisord
supervisor的使用可以參考yang xiang的博客
假設(shè)想守護(hù)frps進(jìn)程剧辐,并令其開機(jī)啟動。在/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上設(shè)置
先附兩個教程
Ubuntu 14.04下進(jìn)程管理工具supervisor安裝
supervisor安裝部署文檔和管理實例