Supervisor的作用與配置:實現對異常中斷的子進程的自動重啟
supervisor管理進程,是通過fork/exec的方式將這些被管理的進程當作supervisor的子進程來啟動,
所以我們只需要將要管理進程的可執行文件的路徑添加到supervisor的配置文件中就好了。
此時被管理進程被視為supervisor的子進程,若該子進程異常中斷,則父進程可以準確的獲取子進程異常中斷的信息,
通過在配置文件中設置autostart=ture,可以實現對異常中斷的子進程的自動重啟。
安裝supervisor
$ sudo apt-get install supervisor
配置文件
安裝完supervisor後,輸入以下命令可得到配置文件:
$ echo_supervisord_conf
或者:
$ cat /etc/supervisord/supervisord.conf
配置文件如下(分號;表示註釋):
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[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
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
以上配置文件用到幾個部分:
[unix_http_server]:這部分設置HTTP服務器監聽的UNIX domain socket
file: 指向UNIX domain socket,即file=/var/run/supervisor.sock
chmod:啟動時改變supervisor.sock的權限
[supervisord]:與supervisord有關的全局配置需要在這部分設置
logfile: 指向記錄supervisord進程的log文件
pidfile:pidfile保存子進程的路徑
childlogdir:子進程log目錄設為AUTO的log目錄
[supervisorctl]:
serverurl:進入supervisord的URL, 對於UNIX domain sockets, 應設為 unix:///absolute/path/to/file.sock
[include]:如果配置文件包含該部分,則該部分必須包含一個files鍵:
files:包含一個或多個文件,這裏包含了/etc/supervisor/conf.d/目錄下所有的.conf文件,可以在該目錄下增加我們自己的配置文件,在該配置文件中增加[program:x]部分,用來運行我們自己的程序,如下:
[program:x]:配置文件必須包括至少一個program,x是program名稱,必須寫上,不能為空
command:包含一個命令,當這個program啟動時執行
directroy:執行子進程時supervisord暫時切換到該目錄
user:賬戶名
startsecs:進程從STARING狀態轉換到RUNNING狀態program所需要保持運行的時間(單位:秒)
redirect_stderr:如果是true,則進程的stderr輸出被發送回其stdout文件描述符上的supervisord
stdout_logfile:將進程stdout輸出到指定文件
stdout_logfile_maxbytes:stdout_logfile指定日誌文件最大字節數,默認為50MB,可以加KB、MB或GB等單位
stdout_logfile_backups:要保存的stdout_logfile備份的數量
示例如下,在目錄/etc/supervisor/conf.d/下創建awesome.conf,並加入:
;/etc/supervisor/conf.d/awesome.conf
[program:awesome]
command = /usr/bin/env python3 /srv/awesome/www/app.py
directory = /srv/awesome/www
user = www-data
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups = 10
stdout_logfile = /srv/awesome/log/app.log
配置完後,先進入/srv/awesome/目錄下創建log目錄,之後啟動supervisor:
$ sudo supervisord -c supervisor.conf
supervisor基本命令(後四個命令可以省略“-c supervisor.conf”):
supervisord -c supervisor.conf 通過配置文件啟動supervisor
supervisorctl -c supervisor.conf status 查看狀態
supervisorctl -c supervisor.conf reload 重新載入配置文件
supervisorctl -c supervisor.conf start [all]|[x] 啟動所有/指定的程序進程
supervisorctl -c supervisor.conf stop [all]|[x] 關閉所有/指定的程序進程
執行服務(運行app.py):
$ sudo supervisorctl start awesome
如果supervisor遇到錯誤,可以在/var/log/supervisor/supervisord.log中查看日誌;
如果app運行出現問題,可以在/srv/awesome/log/app.log中查看日誌。
Supervisor的作用與配置:實現對異常中斷的子進程的自動重啟