Supervisor在Linux上的安裝與配置

 

Supervisor安裝與配置

Supervisor(http://supervisord.org/)是用Python開發的一個client/server服務,是Linux/Unix系統下的一個進程管理工具,不支持Windows系統。它可以很方便的監聽、啓動、停止、重啓一個或多個進程。用Supervisor管理的進程,當一個進程意外被殺死,supervisort監聽到進程死後,會自動將它重新拉起,很方便的做到進程自動恢復的功能,不再需要自己寫shell腳本來控制。

因爲Supervisor是Python開發的,安裝前先檢查一下系統否安裝了Python2.4以上版本。下面以CentOS7,Python2.7版本環境下,介紹Supervisor的安裝與配置步聚:

1、安裝Python包管理工具(easy_install) (1)easy_install是setuptools包裏帶的一個命令,使用easy_install實際上是在調用setuptools來完成安裝模塊的工作,所以安裝setuptools即可。

(2)wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

2、安裝supervisor

yum install python-setuptools

easy_install supervisor

supervisor安裝完成後會生成三個執行程序:supervisortd、supervisorctl、echo_supervisord_conf,分別是supervisor的守護進程服務(用於接收進程管理命令)、客戶端(用於和守護進程通信,發送管理進程的指令)、生成初始配置文件程序。

3、配置

運行supervisord服務的時候,需要指定supervisor配置文件,如果沒有顯示指定,默認在以下目錄查找:

$CWD/supervisord.conf

$CWD/etc/supervisord.conf /etc/supervisord.conf /etc/supervisor/supervisord.conf (since Supervisor 3.3.0) ../etc/supervisord.conf (Relative to the executable) ../supervisord.conf (Relative to the executable)

$CWD表示運行supervisord程序的目錄。

可以通過運行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:

mkdir /etc/supervisor echo_supervisord_conf > /etc/supervisor/supervisord.conf

 

4、配置文件參數說明

supervisor的配置參數較多,下面介紹一下常用的參數配置,詳細的配置及說明,請參考官方文檔介紹。 

注:分號(;)開頭的配置表示註釋

include示例:

[include] files = /opt/absolute/filename.ini /opt/absolute/*.ini foo.conf config??.ini

5、配置管理進程

進程管理配置參數,不建議全都寫在supervisord.conf文件中,應該每個進程寫一個配置文件放在include指定的目錄下包含進supervisord.conf文件中。 

1> 創建/etc/supervisor/config.d目錄,用於存放進程管理的配置文件 

2> 修改/etc/supervisor/supervisord.conf中的include參數,將/etc/supervisor/conf.d目錄添加到include中

[include] files = /etc/supervisor/config.d/*.ini

 

下面是配置Tomcat進程的一個例子:

[program:tomcat] command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out autostart=true autorestart=true startsecs=5 priority=1 stopasgroup=true killasgroup=true

5、啓動Supervisor服務

supervisord -c /etc/supervisor/supervisord.conf

6、控制進程

6.1 交互終端

supervisord啓動成功後,可以通過supervisorctl客戶端控制進程,啓動、停止、重啓。運行supervisorctl命令,不加參數,會進入supervisor客戶端的交互終端,並會列出當前所管理的所有進程。 

 

上圖中的tomcat就是我們在配置文件中[program:tomcat]指定的名字。

輸入help可以查看可以執行的命令列表,如果想看某個命令的作用,運行help 命令名稱,如:help stop

stop tomcat // 表示停止tomcat進程 stop all // 表示停止所有進程 // ...

6.2 bash終端

supervisorctl status supervisorctl stop tomcat supervisorctl start tomcat supervisorctl restart tomcat supervisorctl reread supervisorctl update

6.3 Web管理界面

 

出於安全考慮,默認配置是沒有開啓web管理界面,需要修改supervisord.conf配置文件打開http訪權限,將下面的配置:

 

port:綁定訪問IP和端口,這裏是綁定的是本地IP和9001端口 

username:登錄管理後臺的用戶名 

password:登錄管理後臺的密碼

7、開機啓動Supervisor服務

7.1 配置systemctl服務

1> 進入/lib/systemd/system目錄,並創建supervisor.service文件

[Unit] Description=supervisor After=network.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown ExecReload=/usr/bin/supervisorctl $OPTIONS reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target

2> 設置開機啓動

systemctl enable supervisor.service systemctl daemon-reload

3、修改文件權限爲766

chmod 766 supervisor.service

7.2 配置service類型服務

#!/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 "supervisord -c /etc/supervisor/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

將上述腳本內容保存到/etc/rc.d/init.d/supervisor文件中,修改文件權限爲755,並設置開機啓動

chmod 755 /etc/rc.d/init.d/supervisor chkconfig supervisor on

注意:修改腳本中supervisor配置文件路徑爲你的supervisor的配置文件路徑

注意:

Supervisor只能管理非daemon的進程,也就是說Supervisor不能管理守護進程。否則提示Exited too quickly (process log may have details)異常。例子中的Tomcat默認是以守護進程啓動的,所以我們改成了catalina.sh run,以前臺進程的方式運行。

 

yum方式安裝

yum install epel-release yum install -y supervisor 

supervisor沒有發佈在標準的CentOS源在,需要安裝epel源。這種方式安裝的可能不是最新版本,但比較方便,安裝完成之後,配置文件會自動幫你生成。 

默認配置文件:/etc/supervisord.conf 

進程管理配置文件放到:/etc/supervisord.d/目錄下即可

 

默認日誌文件:/tmp/supervisord.log,可以查看進程的啓動信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章