簡述 Supervisor是用Python開發的一套通用的進程管理程式,能將一個普通的命令行進程變為後臺daemon,並監控進程狀態,異常退出時能自動重啟。 它是通過fork/exec的方式把這些被管理的進程當作supervisor的子進程來啟動,這樣只要在supervisor的配置文件中,把要管理 ...
簡述
Supervisor是用Python開發的一套通用的進程管理程式,能將一個普通的命令行進程變為後臺daemon,並監控進程狀態,異常退出時能自動重啟。
它是通過fork/exec的方式把這些被管理的進程當作supervisor的子進程來啟動,這樣只要在supervisor的配置文件中,把要管理的進程的可執行文件的路徑寫進去即可。
也實現當子進程掛掉的時候,父進程可以準確獲取子進程掛掉的信息的,可以選擇是否自己啟動和報警。supervisor還提供了一個功能,可以為supervisord或者每個子進程,設置一個非root的user,這個user就可以管理它對應的進程。
安裝
Debian / Ubuntu
可以直接通過apt安裝:$ sudo apt-get install supervisor
配置文件
目錄結構
supervisor ├── conf.d │ └── echo_time.conf -- 業務配置文件 └── supervisord.conf -- 主配置文件,一般不需要改動
- 使用
apt-get
安裝後,supervisor
的主配置文件在/etc/supervisor/supervisord.conf
子進程配置文件在
/etc/supervisor/conf.d/*.conf
supervisord 和 supervisorctl的關係
supervisord 是主進程
常用命令
# 使用預設的配置文件 /etc/supervisord.conf supervisord # 明確指定配置文件 supervisord -c /etc/supervisord.conf # 使用 user 用戶啟動 supervisord supervisord -u user
服務命令
- 查看服務狀態:
sudo systemctl status supervisor.service
- 開啟服務:
sudo systemctl start supervisor.service
- 停止服務:
sudo systemctl stop supervisor.service
- 重啟服務:
sudo systemctl restart supervisor.service
- 開機啟動項:
sudo systemctl enable supervisor.service
- 查看服務狀態:
supervisor.service
文件[Unit] Description=Supervisor process control system for UNIX Documentation=http://supervisord.org After=network.target [Service] ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload KillMode=process Restart=on-failure RestartSec=50s [Install] WantedBy=multi-user.target
supervisorctl 是客戶端程式
用於向supervisord
服務發起命令。所有命令,通過
supervisorctl -h
可以查看幫助說明sudo supervisorctl help default commands (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version
控制子進程命令
# 停止某一個進程,program_name 為 [program:x] 里的 x supervisorctl stop program_name # 啟動某個進程 supervisorctl start program_name # 重啟某個進程 supervisorctl restart program_name # 結束所有屬於名為 groupworker 這個分組的進程 (start,restart 同理) supervisorctl stop groupworker: # 結束 groupworker:name1 這個進程 (start,restart 同理) supervisorctl stop groupworker:name1 # 停止全部進程,註:start、restart、stop 都不會載入最新的配置文件 supervisorctl stop all # 載入最新的配置文件,停止原有進程並按新的配置啟動、管理所有進程 supervisorctl reload # 根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟 supervisorctl update
修改進程配置文件後,只要
sudo systemctl restart supervisor.service
則會自動載入哦
示例
以簡單的
/home/www/python/echo_time.sh
為例#/bin/bash while true; do echo `date +%Y-%m-%d,%H:%m:%s` sleep 2 done
在
/etc/supervisor/conf.d/
新增子進程配置文件echo_time.conf
; /etc/supervisor/conf.d/echo_time.conf [program:echo_time] command = /usr/bin/env sh /home/www/python/echo_time.sh directory = /home/www/python user = www startsecs = 3 redirect_stderr = true stdout_logfile_maxbytes = 50MB stdout_logfile_backups = 10 stdout_logfile = /home/www/python/log/echo_time.log
然後啟動程式:
$ supervisorctl reread $ supervisorctl update
這兩個命令分別代表重新讀取配置、更新子進程組
這樣剛纔添加的
echo_time
腳本就常駐運行起來了。可以通過日誌查看運行情況:tail -f log/echo_time.log 2018-12-22,22:12:1545490695 2018-12-22,22:12:1545490697 2018-12-22,22:12:1545490699 2018-12-22,22:12:1545490701
也可以使用
sudo supervisorctl status
查看子進程運行情況:sudo supervisorctl status echo_time RUNNING pid 28906, uptime 0:08:36
web界面操作
需要開啟主配置文件
supervisord.conf
註釋掉的這4行 (/etc/supervisor/supervisord.conf
)[inet_http_server] ; inet (TCP) server disabled by default port=*:9001 ; (ip_address:port specifier, *:port for ;all iface) ;username=www ; (default is no username (open server)) ;password=www123456 ; (default is no password (open server))
註意:如果修改配置文件時, [inet_http_server]這一行被註釋,會導致不僅web需要認證,命令行使用 supervisorctl也需要認證,這時候就需要在互動式命令行里輸入用戶名、密碼才能進行下一步的操作。
瀏覽器訪問:
http://myip:9001
,輸入用戶名、密碼後,即可看到web頁面:
遇到的錯誤
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord
監控工具
參考
- unix:///var/run/supervisor.sock no such file on Ubuntu 16.04
- How To Use Systemctl to Manage Systemd Services and Units
- Supervisor使用教程
- python supervisor使用