supervisor
supervisor管理進(jìn)程,是通過(guò)fork/exec的方式將這些被管理的進(jìn)程當(dāng)作supervisor的子進(jìn)程來(lái)啟動(dòng)悦荒,所以我們只需要將要管理進(jìn)程的可執(zhí)行文件的路徑添加到supervisor的配置文件中就好了。此時(shí)被管理進(jìn)程被視為supervisor的子進(jìn)程搬味,若該子進(jìn)程異常中斷,則父進(jìn)程可以準(zhǔn)確的獲取子進(jìn)程異常中斷的信息身腻,通過(guò)在配置文件中設(shè)置autostart=ture,可以實(shí)現(xiàn)對(duì)異常中斷的子進(jìn)程的自動(dòng)重啟嘀趟。
安裝supervisor
$ sudo apt-get install supervisor
配置文件
安裝完supervisor后,輸入以下命令可得到配置文件:
$ echo_supervisord_conf
或者:
$ cat /etc/supervisord/supervisord.conf
配置文件如下(分號(hào)牛隅;表示注釋):
; 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
以上配置文件用到幾個(gè)部分:
- [unix_http_server]:這部分設(shè)置HTTP服務(wù)器監(jiān)聽(tīng)的UNIX domain socket
- file: 指向UNIX domain socket酌泰,即file=/var/run/supervisor.sock
- chmod:?jiǎn)?dòng)時(shí)改變supervisor.sock的權(quán)限
- [supervisord]:與supervisord有關(guān)的全局配置需要在這部分設(shè)置
- logfile: 指向記錄supervisord進(jìn)程的log文件
- pidfile:pidfile保存子進(jìn)程的路徑
- childlogdir:子進(jìn)程log目錄設(shè)為AUTO的log目錄
- [supervisorctl]:
- serverurl:進(jìn)入supervisord的URL, 對(duì)于UNIX domain sockets, 應(yīng)設(shè)為 unix:///absolute/path/to/file.sock
- [include]:如果配置文件包含該部分陵刹,則該部分必須包含一個(gè)files鍵:
- files:包含一個(gè)或多個(gè)文件,這里包含了/etc/supervisor/conf.d/目錄下所有的.conf文件也糊,可以在該目錄下增加我們自己的配置文件,在該配置文件中增加[program:x]部分狸剃,用來(lái)運(yùn)行我們自己的程序狗热,如下:
- [program:x]:配置文件必須包括至少一個(gè)program钞馁,x是program名稱,必須寫上僧凰,不能為空
- command:包含一個(gè)命令,當(dāng)這個(gè)program啟動(dòng)時(shí)執(zhí)行
- directory:執(zhí)行子進(jìn)程時(shí)supervisord暫時(shí)切換到該目錄
- user:賬戶名
- startsecs:進(jìn)程從STARING狀態(tài)轉(zhuǎn)換到RUNNING狀態(tài)program所需要保持運(yùn)行的時(shí)間(單位:秒)
- redirect_stderr:如果是true膝擂,則進(jìn)程的stderr輸出被發(fā)送回其stdout文件描述符上的supervisord
- stdout_logfile:將進(jìn)程stdout輸出到指定文件
- stdout_logfile_maxbytes:stdout_logfile指定日志文件最大字節(jié)數(shù)隙弛,默認(rèn)為50MB架馋,可以加KB全闷、MB或GB等單位
- stdout_logfile_backups:要保存的stdout_logfile備份的數(shù)量
示例如下,在目錄/etc/supervisor/conf.d/下創(chuàng)建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
配置完后钓瞭,先進(jìn)入/srv/awesome/目錄下創(chuàng)建log目錄,之后啟動(dòng)supervisor:
$ sudo supervisord -c supervisor.conf
supervisor基本命令(后四個(gè)命令可以省略“-c supervisor.conf”):
supervisord -c supervisor.conf 通過(guò)配置文件啟動(dòng)supervisor
supervisorctl -c supervisor.conf status 查看狀態(tài)
supervisorctl -c supervisor.conf reload 重新載入配置文件
supervisorctl -c supervisor.conf start [all]|[x] 啟動(dòng)所有/指定的程序進(jìn)程
supervisorctl -c supervisor.conf stop [all]|[x] 關(guān)閉所有/指定的程序進(jìn)程
執(zhí)行服務(wù)(運(yùn)行app.py):
$ sudo supervisorctl start awesome
如果supervisor遇到錯(cuò)誤山涡,可以在/var/log/supervisor/supervisord.log中查看日志唆迁;
如果app運(yùn)行出現(xiàn)問(wèn)題,可以在/srv/awesome/log/app.log中查看日志唐责。