Supervisor服務開機自啓動

要解決的問題

在機器上部署自己編寫的服務時候,我們可以使用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 <[email protected]> (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  ]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章