Python進程管理神器Supervisor詳解
一、簡介
Supervisor 是一款 Python 開發(fā)的進程管理系統(tǒng),允許用戶監(jiān)視和控制 Linux 上的進程,能將一個普通命令行進程變?yōu)楹笈_守護進程,異常退出時能自動重啟
詳細(xì)介紹查閱:Supervisor Introduction
二、安裝
Supervisor 支持 Linux 和 Mac,不支持 Windows
本文系統(tǒng)為: centos,supervisor==4.2.4
pip3 install supervisor
安裝完成后,在Python bin目錄下面會有這樣幾個文件。
ls /usr/local/Python38/bin echo_supervisord_conf supervisorctl supervisord
注:由于python bin目錄是加入了環(huán)境變量,所以可以直接執(zhí)行這幾個文件。
三、創(chuàng)建配置文件
1.首先建個目錄,存放配置文件:mkdir supervisord。
echo_supervisord_conf > supervisord/supervisord.conf
若報錯 -bash: /etc/supervisord.conf: Permission denied,需要切換到 root用戶。
2.創(chuàng)建子進程配置文件路徑
mkdir -p supervisor/conf.d
后面我們的任務(wù),我們想把它作為守護進程,都需要一個配置文件,我們把這些配置文件放在conf.d目錄下面。
3.修改配置文件
vim supervisord/supervisord.conf
將最后一部分改為
[include] # 因為我這里是放在root用戶目錄下,也可以放其它目錄 files=/root/supervisord/conf.d/*.conf
四、初試
1.啟動 supervisord
supervisord -c supervisord/supervisord.conf
這樣就把supervisord啟動了,我們接下來可以把我們的任務(wù)交給他去守護了,停止了會幫我們自動重啟。
查看版本
supervisord -v
2.編寫簡單的 Shell 腳本
vim supervisord/test.sh
內(nèi)容如下
#!/bin/bash while : do echo `date '+%Y-%m-%d %H:%m:%S'` sleep 1 done
簡單運行,Ctrl + C 退出
3.創(chuàng)建子進程配置文件
vim supervisor/conf.d/test.conf
test.conf內(nèi)容如下:
[program:test] command=sh /root/supervisord/test.sh priority=999 ; 相對啟動優(yōu)先級,數(shù)值越小越優(yōu)先,默認(rèn)為999 autostart=true; 在supervisor啟動時自動啟動,默認(rèn)為true autorestart=true ; 在意外退出時重新啟動,默認(rèn)為true startsecs=10 ; 子進程啟動多少秒后狀態(tài)為running則認(rèn)為啟動成功,默認(rèn)為1 startretries=3; 嘗試啟動的最大次數(shù),默認(rèn)為3 exitcodes=0,2 ; 進程的預(yù)期退出代碼列表,默認(rèn)為0 stopsignal=QUIT; 終止進程的信號,默認(rèn)為TERM stopwaitsecs=10; 在SIGKILL之前等待的最大秒數(shù),默認(rèn)為10 user=root ; 在某用戶下設(shè)置uid來啟動程序,默認(rèn)不切換用戶 redirect_stderr=true ; 是否重定向stdout和stderr,默認(rèn)為false stdout_logfile=/tmp/supervisor.stdout.log ; stdout的輸出文件,默認(rèn)為AUTO stdout_logfile_maxbytes=50MB ; stdout最大文件大小,默認(rèn)為50MB stdout_logfile_backups=10 ; stdout文件備份數(shù),設(shè)為0則不備份,默認(rèn)為10
其實只需要配置3個參數(shù),其它不用管:
command=sh /root/supervisord/test.sh: 我們的子進程啟動命令;stdout_logfile=/tmp/supervisor.stdout.log: 日志;program:test:進程名為test,如果進程哪一天想停止啟動等,需要進程名;
目前文件目錄結(jié)構(gòu)是這樣的:
yum install tree tree supervisord supervisord ├── conf.d │└── test.conf ├── supervisord.conf └── test.sh
4.重新讀取配置并更新子進程
因為我們的supervisord已經(jīng)啟動了,可以通過ps -ef | grep supervisord.conf查看。添加了子進程配置文件,需要把它重新加載進來:
先進入supervisord目錄: cd supervisord,否則執(zhí)行下面命令會有問題。
supervisorctl reread
再次查看進程狀態(tài)
supervisorctl status
結(jié)果:
testRUNNING pid 30278, uptime 1:29:41
名為test的這個進程已經(jīng)作為守護進程在后臺運行,我們來kill掉他:
kill 30278
再執(zhí)行supervisorctl status,會發(fā)現(xiàn)狀態(tài)立馬由starting,不一會變成running,那么supervisord的作用已經(jīng)很明顯了,可以自動幫我們自動監(jiān)控任務(wù)。
注:對于子進程的添加、刪除、啟動、停止相關(guān)命令,見附錄。
五、Web 界面
web界面沒多大用處,就是如果想啟動、暫停進程是,不需要敲命令而已。
vim supervisord.conf
取消注釋
[inet_http_server] port=*:9001 ; 此處改為*便于調(diào)試
重啟 supervisord
supervisorctl reload
瀏覽器訪問:linux_ip:9001.
附錄:supervisorctl 常用命令
新增某配置文件,重新加載
supervisorctl reread
改動某配置文件,重新加載
supervisorctl update
重啟 supervisord
supervisorctl reload
查看所有進程狀態(tài)
supervisorctl status
查看指定進程狀態(tài)
supervisorctl status <name>
啟動所有子進程
supervisorctl start all
啟動指定子進程
supervisorctl start <name>
重啟所有子進程
supervisorctl restart all
重啟指定子進程
supervisorctl restart <name>
停止所有子進程
supervisorctl stop all
停止指定子進程
supervisorctl stop <name>
添加子進程到進程組
supervisorctl add <name>
從進程組移除子進程,需要先stop。注意:移除后,需要使用reread和update才能重新運行該進程
supervisorctl reomve <name>
歡迎訪問我的個人博客,聽聽我的故事。
到此這篇關(guān)于Python進程管理神器Supervisor的文章就介紹到這了,更多相關(guān)PythonSupervisor進程管理內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信