目錄
- 前言
- 安裝
- 生成配置
- 配置文件參數(shù)說(shuō)明
- 配置管理進(jìn)程
- 啟動(dòng)服務(wù)
- 控制進(jìn)程
- 交互終端
- bash終端
- 開機(jī)啟動(dòng)服務(wù)
前言
Supervisor 是用 Python 開發(fā)的一個(gè) client/server 服務(wù),是Linux/Unix系統(tǒng)下的一個(gè)進(jìn)程管理工具,不支持Windows系統(tǒng)。它可以很方便的監(jiān)聽、啟動(dòng)、停止、重啟一個(gè)或多個(gè)進(jìn)程。用Supervisor管理的進(jìn)程,當(dāng)一個(gè)進(jìn)程意外被殺死,supervisort監(jiān)聽到進(jìn)程死后,會(huì)自動(dòng)將它重新拉起,很方便的做到進(jìn)程自動(dòng)恢復(fù)的功能,不再需要自己寫shell腳本來(lái)控制。
因?yàn)?code>Supervisor是 Python 開發(fā)的,安裝前先檢查一下系統(tǒng)否安裝了 Python2.4 以上版本。下面以 CentOS7,Python 3.9 版本環(huán)境下,介紹 Supervisor 的安裝與配置步聚。
安裝
pip3 install supervisor
生成配置
可以通過運(yùn)行echo_supervisord_conf
程序生成 supervisor 的初始化配置文件,如下所示
mkdir /etc/supervisor echo_supervisord_conf > /etc/supervisor/supervisord.conf
配置文件參數(shù)說(shuō)明
supervisor
的配置參數(shù)較多,下面介紹一下常用的參數(shù)配置
分號(hào)(;)開頭的配置表示注釋
[unix_http_server] file=/tmp/supervisor.sock ;UNIX socket 文件,supervisorctl 會(huì)使用 ;chmod=0700 ;socket文件的mode,默認(rèn)是0700 ;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid ;[inet_http_server] ;HTTP服務(wù)器,提供web管理界面 ;port=127.0.0.1:9001 ;Web管理后臺(tái)運(yùn)行的IP和端口,如果開放到公網(wǎng),需要注意安全性 ;username=user ;登錄管理后臺(tái)的用戶名 ;password=123 ;登錄管理后臺(tái)的密碼 [supervisord] logfile=/tmp/supervisord.log ;日志文件,默認(rèn)是 $CWD/supervisord.log logfile_maxbytes=50MB ;日志文件大小,超出會(huì)rotate,默認(rèn) 50MB,如果設(shè)成0,表示不限制大小 logfile_backups=10 ;日志文件保留備份數(shù)量默認(rèn)10,設(shè)為0表示不備份 loglevel=info ;日志級(jí)別,默認(rèn)info,其它: debug,warn,trace pidfile=/tmp/supervisord.pid ;pid 文件 nodaemon=false ;是否在前臺(tái)啟動(dòng),默認(rèn)是false,即以 daemon 的方式啟動(dòng) minfds=1024 ;可以打開的文件描述符的最小值,默認(rèn) 1024 minprocs=200 ;可以打開的進(jìn)程數(shù)的最小值,默認(rèn) 200 [supervisorctl] serverurl=unix:///tmp/supervisor.sock ;通過UNIX socket連接supervisord,路徑與unix_http_server部分的file一致 ;serverurl=http://127.0.0.1:9001 ; 通過HTTP的方式連接supervisord ;[program:xx]是被管理的進(jìn)程配置參數(shù),xx是進(jìn)程的名稱 [program:xx] command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序啟動(dòng)命令 autostart=true ; 在supervisord啟動(dòng)的時(shí)候也自動(dòng)啟動(dòng) startsecs=10 ; 啟動(dòng)10秒后沒有異常退出,就表示進(jìn)程正常啟動(dòng)了,默認(rèn)為1秒 autorestart=true ; 程序退出后自動(dòng)重啟,可選值:[unexpected,true,false],默認(rèn)為unexpected,表示進(jìn)程意外殺死后才重啟 startretries=3 ; 啟動(dòng)失敗自動(dòng)重試次數(shù),默認(rèn)是3 user=tomcat ; 用哪個(gè)用戶啟動(dòng)進(jìn)程,默認(rèn)是root priority=999 ; 進(jìn)程啟動(dòng)優(yōu)先級(jí),默認(rèn)999,值小的優(yōu)先啟動(dòng) redirect_stderr=true ; 把stderr重定向到stdout,默認(rèn)false stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默認(rèn)50MB stdout_logfile_backups = 20 ; stdout 日志文件備份數(shù),默認(rèn)是10 ; stdout 日志文件,需要注意當(dāng)指定目錄不存在時(shí)無(wú)法正常啟動(dòng),所以需要手動(dòng)創(chuàng)建目錄(supervisord 會(huì)自動(dòng)創(chuàng)建日志文件) stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out stopasgroup=false ;默認(rèn)為false,進(jìn)程被殺死時(shí),是否向這個(gè)進(jìn)程組發(fā)送stop信號(hào),包括子進(jìn)程 killasgroup=false ;默認(rèn)為false,向進(jìn)程組發(fā)送kill信號(hào),包括子進(jìn)程 ;包含其它配置文件 [include] files = relative/directory/*.ini ;可以指定一個(gè)或多個(gè)以.ini結(jié)束的配置文件
配置管理進(jìn)程
進(jìn)程管理配置參數(shù),不建議全都寫在supervisord.conf
文件中,應(yīng)該每個(gè)進(jìn)程寫一個(gè)配置文件放在include
指定的目錄下包含進(jìn)supervisord.conf
文件中。
- 創(chuàng)建
/etc/supervisor/config.d
目錄,用于存放進(jìn)程管理的配置文件 - 修改
/etc/supervisor/supervisord.conf
中的include
參數(shù),將/etc/supervisor/conf.d
目錄添加到include
中
[include] files = /etc/supervisor/config.d/*.ini
下面是一個(gè) Python 的例子
[program:wechat] command=/root/.virtualenvs/wechat-robot/bin/python /root/wechat-robot/server.py stdout_logfile=/root/wechat-robot/log/stdout.log autostart=true autorestart=true startsecs=5 priority=1 stopasgroup=true killasgroup=true
啟動(dòng)服務(wù)
supervisord -c /etc/supervisor/supervisord.conf
控制進(jìn)程
交互終端
supervisord
啟動(dòng)成功后,可以通過supervisorctl
客戶端控制進(jìn)程,啟動(dòng)、停止、重啟。運(yùn)行supervisorctl命令,不加參數(shù),會(huì)進(jìn)入supervisor
客戶端的交互終端,并會(huì)列出當(dāng)前所管理的所有進(jìn)程。
(wechat-robot) [root@VM-46-61-centos ~]# supervisorctl wechat RUNNING pid 29346, uptime 0:44:10 supervisor> status wechat wechat RUNNING pid 29346, uptime 0:44:17 supervisor> help default commands (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version supervisor> help stop stop <name> Stop a process stop <gname>:* Stop all processes in a group stop <name> <name> Stop multiple processes or groups stop all Stop all processes supervisor>
bash終端
supervisorctl status supervisorctl stop tomcat supervisorctl start tomcat supervisorctl restart tomcat supervisorctl reread supervisorctl update
(wechat-robot) [root@VM-46-61-centos ~]# supervisorctl status wechat wechat RUNNING pid 29346, uptime 0:45:12
開機(jī)啟動(dòng)服務(wù)
# vim /lib/systemd/system/supervisor.service [Unit] Description=supervisor After=network.target [Service] Type=forking ExecStart=/usr/local/bin/supervisord -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
修改文件權(quán)限為766
chmod 766 supervisor.service
設(shè)置開機(jī)啟動(dòng)
systemctl enable supervisor.service systemctl daemon-reload
以上就是Linux 進(jìn)程管理工具之Supervisor安裝配置的詳細(xì)內(nèi)容,更多關(guān)于Linux 進(jìn)程管理的資料請(qǐng)關(guān)注其它相關(guān)文章!