要解決的問題
在機器上部署自己編寫的服務時候,我們可以使用Supervisor
作為進程檢活工具,用來自動重啟服務。
但是當機器重啟后,Supervisor卻不能自動重啟,那么誰來解決這個問題呢?
答案就是linux的service。
總體思路
編寫一個腳本,然后把它放在/etc/init.d這個目錄下,再用service + 腳本名字 運行即可。如果是要開機自動啟動那就得用chkconfig命令了。
話不多說,上手做吧!
安裝方法
增加service配置
[root@hdx9whvy init.d]# vim /etc/init.d/supervisor
內容為
#!/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/supervisor/supervisord.conf
# pidfile: /var/run/supervisord.pid
#
# source function library
. /etc/rc.d/init.d/functions
RETVAL=0
start() {
echo -n $"Starting supervisord: "
daemon "/usr/bin/supervisord -c /etc/supervisord.conf"
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
設置腳本為可執行
[root@hdx9whvy init.d]# chmod +x /etc/init.d/supervisor
設置服務開機啟動
- service這個命令往往是即時生效,不用開關機,但是重啟后服務會回到默認狀態。
- chkconfig是用於把服務加到開機自動啟動列表里,只要啟動它,就能自動啟動,重啟后永久生效
[root@hdx9whvy init.d]# chkconfig supervisor on
啟停命令
[root@hdx9whvy init.d]# service supervisor start
Starting supervisord: [ OK ]
[root@hdx9whvy init.d]# service supervisor stop
Stopping supervisord: [ OK ]
[root@hdx9whvy ~]# service supervisor restart
Stopping supervisord: [ OK ]
Starting supervisord: [ OK ]