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

