What is supervisor
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
引用自supervisor官網,中文意思就是是一個C/S架構的系統,用來監控管理類UNIX系統上進程。
Features
簡單,高效,可擴展,兼容性好(Orz,其實不能在windows上用)
想看更多請去官網
Installing
- 因為是用Python實現的,所以最簡單的方式是
pip install supervisor
- ubuntu系統上也可以直接使用
sudo apt-get install supervisor
Configure
- 使用
echo_supervisord_conf
就可以看到默認的配置文件,如下:
➜ Log echo_supervisord_conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
- 配置文件
使用 echo_supervisord_conf > /etc/supervisord.conf
命令將配置文件保存在xx下面,然后修改配置文件。推薦的方式是將最后一行修改到某個固定文件夾,如下:
[include]
files = /etc/supervisord.d/*.ini
這樣每次運行都會加載此目錄下的配置文件,每個文件單獨管理一個進程。而*.ini的內容一般如下:
配置1
[program:simpleserver]
command=python -m SimpleHTTPServer # 執行的命令 ,若是虛擬環境則需要注意命令的路徑,見配置2
directory=/home/wang/Downloads # 執行命令的路徑
user=wang # 執行命令的用戶
autorestart=true # 出錯后自動重啟
redirect_stderr=true # 錯誤日志重定向
stdout_logfile=/home/wang/Log/SimpleHTTPServer.log # 日志的路徑
loglevel=info # 日志的級別
配置2
[program:hongbaoyun]
command=/home/wang/.virtualenvs/xxx-virtual-env/bin/python manage.py runserver 0.0.0.0:9999 # 此處python位置是virtualenv中python的位置
directory=/home/wang/Workspace/khb/hongbaoyun
user=wang
Run
-
啟動
supervisord -c supervisord.conf # 指定配置文件啟動supervisord
-
啟動spuervisordctl
supervisordctl
-
supervisordctl常用命令
supervisorctl stop program_name # 停止某一個進程,program_name 為 [program:x] 里的 x
supervisorctl start program_name # 啟動某個進程
supervisorctl restart program_name # 重啟某個進程
supervisorctl stop groupworker: # 結束所有屬於名為 groupworker 這個分組的進程 (start,restart 同理)
supervisorctl stop groupworker:name1 # 結束 groupworker:name1 這個進程 (start,restart 同理)
supervisorctl stop all # 停止全部進程,注:start、restartUnlinking stale socket /tmp/supervisor.sock
、stop 都不會載入最新的配置文件
supervisorctl reload # 載入最新的配置文件,停止原有進程並按新的配置啟動、管理所有進程
supervisorctl update # 根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
- 圖行管理界面
在配置文件中去掉 [inet http server]的注釋就可在瀏覽器中通過127.0.0.1:8000中看到圖形管理界面
FAQ
-
可能會出現 Unlinking stale socket /tmp/supervisor.sock 的錯誤,解決方式見 http://stackoverflow.com/questions/14479894/stopping-supervisord-shut-down, 是配置文件的問題
-
開機自動啟動,見 http://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu
Refer
-
http://www.restran.net/2015/10/04/supervisord-tutorial/ (很詳細,推薦看)
-
http://stackoverflow.com/questions/14479894/stopping-supervisord-shut-down
-
http://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu