python進(jìn)程管理工具supervisor安裝使用
1、概述
supervisor是一個(gè)用python語言編寫的進(jìn)程管理工具,它可以很方便的監(jiān)聽、啟動(dòng)、停止、重啟一個(gè)或多個(gè)進(jìn)程。當(dāng)一個(gè)進(jìn)程意外被殺死,supervisor監(jiān)聽到進(jìn)程死后,可以很方便的讓進(jìn)程自動(dòng)恢復(fù),不再需要程序員或系統(tǒng)管理員自己編寫代碼來控制。
2、架構(gòu):三大構(gòu)成要素
- supervisord
supervisor的服務(wù)端:運(yùn)行supervisor時(shí)會(huì)啟動(dòng)一個(gè)進(jìn)程supervisord,它負(fù)責(zé)啟動(dòng)所管理的進(jìn)程,并將所管理的進(jìn)程作為自己的子進(jìn)程來啟動(dòng),而且可以在所管理的進(jìn)程出現(xiàn)崩潰時(shí)自動(dòng)重啟
- supervisorctl
supervisor的客戶端:supervisorctl是命令行管理工具,可以用命令來進(jìn)行子進(jìn)程的管理,supervisorctl常見命令
命令 | 含義 |
---|---|
supervisorctl status | 查看所有子進(jìn)程服務(wù)狀態(tài) |
supervisorctl restart | 重啟所有子進(jìn)程服務(wù) |
supervisorctl restart name | 重啟子進(jìn)程名字為name的服務(wù) |
supervisorctl start name | 開啟子進(jìn)程名字為name的服務(wù) |
supervisorctl stop all | 關(guān)閉所有子進(jìn)程服務(wù) |
supervisorctl stop name | 停止子進(jìn)程名字為name的服務(wù) |
supervisorctl shutdown | 關(guān)閉所有子進(jìn)程服務(wù) |
supervisorctl reload | 重載配置文件,重啟所有子進(jìn)程服務(wù) |
supervisorctl update | 更新所有服務(wù),一般用在添加新服務(wù)后 |
supervisorctl update name | 更新子進(jìn)程名字為name服務(wù) |
- echo_supervisord_conf
默認(rèn)的配置文件,一般生成默認(rèn)文件為 supervisor.conf
3、安裝
3.1、supervisor是基于python寫的,所以使用pip來安裝即可。
pip install supervisor
3.2、默認(rèn)生成的幾個(gè)地址需要我們關(guān)注
# supervisord 路徑 /usr/local/bin/supervisord # supervisorctl 路徑 /usr/local/bin/supervisorctl # echo_supervisord_conf 路徑 /usr/local/bin/echo_supervisord_conf
如上路徑,我們也可以通過 whereis supervisord
、 whereis supervisorctl
、 whereis echo_supervisord_conf
得到
3.3、驗(yàn)證是否安裝成功
supervisorctl --help
4、配置
4.1、創(chuàng)建 /etc/supervisor 目錄
mkdir /etc/supervisor
???????4.2、創(chuàng)建并修改supervisord.conf文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
vi /etc/supervisor/supervisord.conf
# 將unix_http_server 下的 file 路徑改成如下內(nèi)容 [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) # 將supervisord 下的logfile 路徑 和 pidfile 路徑改成如下內(nèi)容 [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file; default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile; default supervisord.pid) [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; (use a unix:// URL for a unix socket) # 將include 取消注釋并將其下的 files 路徑改成如下內(nèi)容。標(biāo)記著supervisor將會(huì)默認(rèn)運(yùn)行/etc/supervisor/conf.d的所有conf配置文件 [include] files = /etc/supervisor/conf.d/*.conf
注:上面的路徑只是推薦路徑,你也可以根據(jù)自己的想法,指向不同路徑
4.3、創(chuàng)建并添加文件權(quán)限(上文提到的)
# 創(chuàng)建文件 #touch /var/run/supervisor.sock # 依據(jù)配置文件自動(dòng)創(chuàng)建 mkdir /var/log/supervisor touch /var/log/supervisor/supervisord.log #touch /var/run/supervisord.pid # 依據(jù)配置文件自動(dòng)創(chuàng)建 mkdir /etc/supervisor/conf.d # 添加權(quán)限 #chmod 777 /var/run #chmod 777 /var/log
4.4、配置supervisor開機(jī)自動(dòng)啟動(dòng)服務(wù)(非必須,按需選擇)
4.4.1、編輯文件(一般自帶,不需要配置) vim /usr/lib/systemd/system/supervisord.service
supervisord.service 文件內(nèi)容如下:
[Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/local/bin/supervisorctl shutdown ExecReload=/usr/local/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
4.4.2、使能服務(wù) systemctl enable supervisord
???????4.4.3、驗(yàn)證是否使能成功
方法一,出現(xiàn)enable說明成功
systemctl is-enabled supervisord
???????方法二,開關(guān)機(jī)驗(yàn)證
注:supervisor常用命令
命令 | 含義 |
---|---|
service supervisord start | 啟動(dòng)supervisor服務(wù) |
service supervisord stop | 停止supervisor服務(wù) |
service supervisord status | 查看supervisor狀態(tài) |
5、如何配置新服務(wù)并系統(tǒng)控制?
假設(shè)服務(wù)名稱為test。啟動(dòng)文件為py類文件entry.py
5.1、創(chuàng)建test.conf并編輯配置文件
vi /etc/supervisor/conf.d/test.conf
[program:test] # 服務(wù)名稱test directory = /home/test_project # 項(xiàng)目路徑,項(xiàng)目運(yùn)行前,會(huì)先切換到這個(gè)目錄 command= /home/test_project/entry.py # 程序入口主文件絕對(duì)路徑 autostart=true # 如果是true的話,子進(jìn)程將在supervisord啟動(dòng)后被自動(dòng)啟動(dòng),默認(rèn)就是true autorestart=true # 子進(jìn)程掛掉后自動(dòng)重啟的情況,有三個(gè)選項(xiàng),false,unexpected和true。false表示無論什么情況下,都不會(huì)被重新啟動(dòng);unexpected表示只有當(dāng)進(jìn)程的退出碼不在下面的exitcodes里面定義的退出碼的時(shí)候,才會(huì)被自動(dòng)重啟。當(dāng)為true的時(shí)候,只要子進(jìn)程掛掉,將會(huì)被無條件的重啟 user=root # root用戶執(zhí)行 redirect_stderr=true # 將stderr重定向stdout,默認(rèn)false,與stderr_logfile互斥 startsecs = 5 # 子進(jìn)程啟動(dòng)多少秒之后,此時(shí)狀態(tài)如果是running,我們認(rèn)為啟動(dòng)成功了,默認(rèn)值1 startretries=5 # 當(dāng)進(jìn)程啟動(dòng)失敗后,最大嘗試的次數(shù)。當(dāng)超過5次后,進(jìn)程的狀態(tài)變?yōu)镕AIL stdout_logfile = None # 正常日志輸出文件,None表示不輸出 stderr_logfile = None # 錯(cuò)誤日志輸出文件,None表示不輸出
5.2、使用supervisorctl客戶端查看程序啟動(dòng)的狀態(tài)前需要先啟動(dòng)supervisor服務(wù)(使用supervisord)
supervisord -c /etc/supervisor/supervisord.conf # 啟動(dòng)supervisor服務(wù) # 如果出現(xiàn) supervisorctl -c /etc/supervisor/supervisord.conf status # 查看程序啟動(dòng)的狀態(tài)
6、便捷
6.1、如上所示,下載supervisor后,啟動(dòng)項(xiàng)目需要以下幾個(gè)步驟:
- 編輯supervisor配置文件,如supervisord.conf
- 創(chuàng)建文件夾及文件,如文件夾/var/log/supervisor、/etc/supervisor/conf.d,文件/var/run/supervisor.sock等
- 賦值權(quán)限,如chmod 777 /var/run
- 編輯程序配置文件,如/etc/supervisor/conf.d/test.conf
- 啟動(dòng),如supervisord -c /etc/supervisor/supervisord.conf
而此時(shí)是有一個(gè)更好的方法,將①④合到一處。①中只關(guān)注重要的一些選項(xiàng)。④中照搬即可 vi /etc/supervisor/conf.d/test.conf
[unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file; default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile; default supervisord.pid) [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; (use a unix:// URL for a unix socket) [include] files = /etc/supervisor/conf.d/*.conf [program:test] # 服務(wù)名稱test directory = /home/test_project # 項(xiàng)目路徑,項(xiàng)目運(yùn)行前,會(huì)先切換到這個(gè)目錄 command= /home/test_project/entry.py # 程序入口主文件絕對(duì)路徑 autostart=true # 如果是true的話,子進(jìn)程將在supervisord啟動(dòng)后被自動(dòng)啟動(dòng),默認(rèn)就是true autorestart=true # 子進(jìn)程掛掉后自動(dòng)重啟的情況,有三個(gè)選項(xiàng),false,unexpected和true。false表示無論什么情況下,都不會(huì)被重新啟動(dòng);unexpected表示只有當(dāng)進(jìn)程的退出碼不在下面的exitcodes里面定義的退出碼的時(shí)候,才會(huì)被自動(dòng)重啟。當(dāng)為true的時(shí)候,只要子進(jìn)程掛掉,將會(huì)被無條件的重啟 user=root # root用戶執(zhí)行 redirect_stderr=true # 將stderr重定向stdout,默認(rèn)false,與stderr_logfile互斥 startsecs = 5 # 子進(jìn)程啟動(dòng)多少秒之后,此時(shí)狀態(tài)如果是running,我們認(rèn)為啟動(dòng)成功了,默認(rèn)值1 startretries=5 # 當(dāng)進(jìn)程啟動(dòng)失敗后,最大嘗試的次數(shù)。當(dāng)超過5次后,進(jìn)程的狀態(tài)變?yōu)镕AIL stdout_logfile = None # 正常日志輸出文件,None表示不輸出 stderr_logfile = None # 錯(cuò)誤日志輸出文件,None表示不輸出
6.2、此時(shí)需要做的步驟
- 編輯系統(tǒng)配置文件與程序配置文件,6.1中已配置完畢
- 創(chuàng)建文件夾及文件,參考上
- 文賦值權(quán)限,參考上文
- 啟動(dòng),此時(shí)的啟動(dòng)命令就由原來的
supervisord -c /etc/supervisor/supervisord.conf
變?yōu)楝F(xiàn)在的supervisord -c /etc/supervisor/conf.d/test.conf
到此這篇關(guān)于python進(jìn)程管理工具supervisor安裝使用的文章就介紹到這了,更多相關(guān)python supervisor使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python測試開發(fā)django之使用supervisord?后臺(tái)啟動(dòng)celery?服務(wù)(worker/beat)
- 在python3中使用Supervisor的詳細(xì)教程
- 使用 Supervisor 監(jiān)控 Python3 進(jìn)程方式
- Python supervisor強(qiáng)大的進(jìn)程管理工具的使用
- python進(jìn)程管理工具supervisor的安裝與使用教程
- 基于Python 的進(jìn)程管理工具supervisor使用指南
- Python使用Supervisor來管理進(jìn)程的方法
- python進(jìn)程管理工具supervisor使用實(shí)例
- 使用Python的Supervisor進(jìn)行進(jìn)程監(jiān)控以及自動(dòng)啟動(dòng)
相關(guān)文章
python numpy庫np.percentile用法說明
這篇文章主要介紹了python numpy庫np.percentile用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python3爬蟲里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07Python爬蟲實(shí)現(xiàn)的根據(jù)分類爬取豆瓣電影信息功能示例
這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)的根據(jù)分類爬取豆瓣電影信息功能,結(jié)合完整實(shí)例形式分析了Python針對(duì)電影信息分類抓取的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09Python+OpenCV實(shí)現(xiàn)角度測量的示例代碼
本文介紹如何使用python語言實(shí)現(xiàn)角度測量,程序包括鼠標(biāo)選點(diǎn)、直線斜率計(jì)算、角度計(jì)算三個(gè)子程序和一個(gè)主程序,感興趣的可以了解一下2022-03-03現(xiàn)代Python編程的四個(gè)關(guān)鍵點(diǎn)你知道幾個(gè)
這篇文章主要為大家詳細(xì)介紹了Python編程的四個(gè)關(guān)鍵點(diǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02