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常見命令
- 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常用命令
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