在linux服務需要可靠的運行,並且服務exit後,需要自動重新啓動起來,還可以監控進程狀態,supervisor就是個不錯的選項,supervisor是用python開發的一個進程管理器。下面是安裝配置過程。
1.安裝supervisor
easy_install supervisor SetupTool安裝方法
apt-get install supervisor Debian/Ubuntu安裝方法
yum install supervisor RedHat/Centos安裝方法
2.生成默認配置文件
echo_supervisord_conf > /etc/supervisord.conf
3.修改/etc/supervisord.conf配置文件
如需要訪問web控制界面,inet_http_server區段修改爲
[inet_http_server]
port=0.0.0.0:9001
username=username ; 你的用戶名
password=password ; 你的密碼
4.修改啓動腳本-將supervisord加入系統服務
#!/bin/sh
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
# Source init functions
. /etc/init.d/functions
RETVAL=0
prog="supervisord"
pidfile="/tmp/supervisord.pid"
lockfile="/var/lock/subsys/supervisord"
start()
{
echo -n $"Starting $prog: "
daemon --pidfile $pidfile supervisord -c /etc/supervisord.conf
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop()
{
echo -n $"Shutting down $prog: "
killproc -p ${pidfile} /usr/bin/supervisord
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f ${lockfile} ${pidfile}
fi
}
case "$1" in
start)
start ;;
stop) stop ;;
status)
status $prog ;;
restart)
stop
start ;;
*)
echo "Usage: $0 {start|stop|restart|status}" ;;
esac
5.賦予權限加到開機啓動
chmod +x /etc/init.d/supervisord
chkconfig supervisord on
service supervisord start
目前所有的supervisor基本的都完成,那麼我現在用httpd來受被管理測試是否可以啓動 並訪問和exit後會不會自啓動。
6.修改/etc/supervisord.conf在最後添加以下內容
[program:apache]
command=/usr/local/apache/bin/httpd -D FOREGROUND
autostart=true
autorestart=true
startsecs=3
user=root
7.重新加載配置文件是否80端口可以訪問
service supervisord restart
日誌顯示正常:
CRIT Supervisor running as root (no user in config file)
INFO RPC interface 'supervisor' initialized
INFO RPC interface 'supervisor' initialized
CRIT Server 'unix_http_server' running without any HTTP authentication checking
INFO daemonizing the supervisord process
INFO supervisord started with pid 4510
INFO spawned: 'apache' with pid 4512
INFO success: apache entered RUNNING state, process has stayed up for > than 3 seconds (startsecs)
8.測試服務
9.supervisor web測試是否顯現正常
10.關閉掉httpd 看是否可以自啓動起來
/usr/local/apache/bin/apachectl stop手動關閉掉httpd
看日誌停止後又啓動起來:
exited: apache (exit status 0; expected)
INFO spawned: 'apache' with pid 4640
INFO success: apache entered RUNNING state, process has stayed up for > than 3 seconds (startsecs)
另外一種配置文件方法(這樣的方法適合多個服務好管理):
1. 修改配置文件 vim /etc/supervisord.conf
[include]
;files = relative/directory/*.ini
修改爲:
[include]
;files = relative/directory/*.ini
files = /etc/supervisord.conf.d/*.conf
2. 創建被管理服務配置文件
Mkdir /etc/supervisord.conf.d/
Cd /etc/supervisord.conf.d/
Vim apache.conf
[program:apache]
command=/usr/local/apache/bin/httpd -D FOREGROUND
autostart=true
autorestart=true
startsecs=3
user=root
3. 常用命令
supervisorctl start apache
supervisorctl stop apache
supervisorctl status apache