supervisor守護進程管理實操筆記

2020年年後工作中需開發一支持多數據源自動上報業務數據的程序,程序開發完部署上線時需要對其進程進行自動管理,不然哪天程序down了還不知,可就麻煩了,所以這裏選用了強大的supervisor,以下文章爲學習及實操時的筆記,不正確處請指出

Supervisor簡介

Supervisor官網:http://supervisord.org
Supervisor是用Python開發的一套通用的進程管理程序,能將一個普通的命令行進程變爲後臺daemon,並監控進程狀態,異常退出時能自動重啓。它是通過fork/exec的方式把這些被管理的進程當作supervisor的子進程來啓動,這樣只要在supervisor的配置文件中,把要管理的進程的可執行文件的路徑寫進去即可。也實現當子進程掛掉的時候,父進程可以準確獲取子進程掛掉的信息的,可以選擇是否自己啓動和報警。
supervisor還提供了一個功能,可以爲supervisord或者每個子進程,設置一個非root的user,這個user就可以管理它對應的進程。

supervisor安裝

1. pip安裝方式

[maaiqiang@localhost ~]$ pip install supervisor

2. 離線包安裝方式 ( 下載離線包https://pypi.org/project/supervisor/#files)

[maaiqiang@localhost ~]$ ls
supervisor-4.2.0.tar.gz

[maaiqiang@localhost ~]$ tar xf supervisor-4.2.0.tar.gz
[maaiqiang@localhost ~]$ cd supervisor-4.2.0
[maaiqiang@localhost ~]$ python setup.py install

3. 等等

supervisor.conf配置文件說明

[unix_http_server]
file=/tmp/supervisor.sock   ;UNIX socket 文件,supervisorctl 會使用
;chmod=0700                 ;socket文件的mode,默認是0700
;chown=nobody:nogroup       ;socket文件的owner,格式:uid:gid

;[inet_http_server]         ;HTTP服務器,提供web管理界面
;port=127.0.0.1:9001        ;Web管理後臺運行的IP和端口,如果開放到公網,需要注意安全性
;username=user              ;登錄管理後臺的用戶名
;password=123               ;登錄管理後臺的密碼

[supervisord]
logfile=/tmp/supervisord.log ;日誌文件,默認是 $CWD/supervisord.log
logfile_maxbytes=50MB        ;日誌文件大小,超出會rotate,默認 50MB,如果設成0,表示不限制大小
logfile_backups=10           ;日誌文件保留備份數量默認10,設爲0表示不備份
loglevel=info                ;日誌級別,默認info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=false               ;是否在前臺啓動,默認是false,即以 daemon 的方式啓動
minfds=1024                  ;可以打開的文件描述符的最小值,默認 1024
minprocs=200                 ;可以打開的進程數的最小值,默認 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]是被管理的進程配置參數,xx是進程的名稱
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; 程序啓動命令
autostart=true       ; 在supervisord啓動的時候也自動啓動
startsecs=10         ; 啓動10秒後沒有異常退出,就表示進程正常啓動了,默認爲1秒
autorestart=true     ; 程序退出後自動重啓,可選值:[unexpected,true,false],默認爲unexpected,表示進程意外殺死後才重啓
startretries=3       ; 啓動失敗自動重試次數,默認是3
user=tomcat          ; 用哪個用戶啓動進程,默認是root
priority=999         ; 進程啓動優先級,默認999,值小的優先啓動
redirect_stderr=true ; 把stderr重定向到stdout,默認false
stdout_logfile_maxbytes=20MB  ; stdout 日誌文件大小,默認50MB
stdout_logfile_backups = 20   ; stdout 日誌文件備份數,默認是10
; stdout 日誌文件,需要注意當指定目錄不存在時無法正常啓動,所以需要手動創建目錄(supervisord 會自動創建日誌文件)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false     ;默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程
killasgroup=false     ;默認爲false,向進程組發送kill信號,包括子進程

;包含其它配置文件
[include]
files = relative/directory/*.ini    ;可以指定一個或多個以.ini結束的配置文件

supervisor配置

1. 通過運行echo_supervisord_conf 程序生成supervisor的初始化配置文件

[maaiqiang@localhost ~]$ mkdir /etc/supervisor
[maaiqiang@localhost ~]$ echo_supervisord_conf > /etc/supervisor/supervisored.conf

2. 編輯 /etc/supervisor/supervisored.conf,在文件結尾處追加如下配置

;conf.d 爲配置表目錄,需要手動創建,方便各子進程配置管理
[include]
files = conf.d/*.conf

3. 創建conf.d目錄

[maaiqiang@localhost ~]$ mkdir /etc/supervisor/conf.d

4. 爲你的程序創建一個.conf文件子進程配置文件,放在目錄"/etc/supervisor/conf.d/"下

[maaiqiang@localhost ~]$ vi UmsScheduler.conf

[program:UmsScheduler]
command=/usr/bin/python /home/mq/ums/py/UmsMonitor/UmsScheduler.py
directory=/home/mq/ums/py/UmsMonitor/

autostart=true
autorestart=true

stderr_logfile=/home/mq/ums/py/UmsMonitor/log/UmsScheduler-err.log
stdout_logfile=/home/mq/ums/py/UmsMonitor/log/UmsScheduler-out.log

user=mq

5. 運行supervisord,查看是否生效

[maaiqiang@localhost ~]$ supervisord -c /etc/supervisor/supervisord.conf

6. supervisor配置開啓自啓動

6.1. 新建/usr/lib/systemd/system/supervisord.service文件
# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
6.2.執行以下命令,方便進行管理
[maaiqiang@localhost ~]$ systemctl enable supervisord.service  # 加入開啓自啓動項
[maaiqiang@localhost ~]$ systemctl is-enabled supervisord   #檢查是否爲開啓啓動項

[maaiqiang@localhost ~]$ systemctl start supervisord.service   # 啓動並加載默認配置文件

7. 驗證supervisord進程是否啓動

[maaiqiang@localhost ~]$ ps -ef | grep 'supervisord'

8. 驗證UmsScheduler子進程是否啓動

[maaiqiang@localhost ~]$ ps -ef | grep 'UmsScheduler'

這裏,可以模擬發生異常將其進程殺死,查看其進程是否能夠自動啓動

supervisor管理命令說明

supervisorctl status        # 查看所有進程的狀態

supervisorctl stop UmsScheduler # 停止UmsScheduler
supervisorctl start UmsScheduler    # 啓動UmsScheduler
supervisorctl restart UmsScheduler        # 重啓UmsScheduler

supervisorctl update       # 配置文件修改後使用命令加載新的配置
supervisorctl reload        # 重新啓動配置中的所有程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章