使用 Supervisor 進(jìn)行進(jìn)程管理
簡介
Supervisor
Supervisor 是可以在類 UNIX 系統(tǒng)中進(jìn)行管理和監(jiān)控各種進(jìn)程的小型系統(tǒng)低滩。它自帶了客戶端和服務(wù)端工具。它可以通過用戶所定義的配置文件來管理和監(jiān)控單個或多個進(jìn)程,并且它可以根據(jù)配置來對異常崩潰的進(jìn)程進(jìn)行重啟操作动羽。
我們可以使用 Supervisor 來輕松的管理和維護(hù) nginx redis node 的進(jìn)程,當(dāng)這些進(jìn)程因為某些原因出現(xiàn)異常而崩潰時渔期,Supervisor 會自動的幫助它重新啟動运吓。
安裝
如果你安裝了 Python 的 setuptools 模塊,那么你就可以使用 easy_install
來進(jìn)行安裝疯趟,這是首選的安裝方式:
easy_install supervisor
或者你也可以通過 pip
:
pip install supervisor
生成配置文件
當(dāng)你安裝完成 Supervisor 之后拘哨,你就可以使用 echo_supervisord_conf
命令來輸出一個配置文件:
echo_supervisord_conf > /etc/supervisord.conf
當(dāng)然你可以將配置文件生成到任意位置,但是你需要在啟動時使用 -c
選項來指定配置文件的路徑信峻。
配置
配置選項:
; 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".
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
;umask=022 ; (process file creation umask;default 022)
;user=chrism ; (default is current user, required if root)
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY="value" ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
; 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:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; 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 = relative/directory/*.ini
我們只需要關(guān)注 program
配置項倦青,每個 program
代表了一個要進(jìn)行管理的進(jìn)程,我們可以在這里進(jìn)行進(jìn)程的相關(guān)配置:
; * 為必選項
;[program:theprogramname]
;command=/bin/cat ; * 要執(zhí)行的命令路徑(可以是相對與 PATH 的路徑)也可帶參數(shù)
;process_name=%(program_name)s ; 當(dāng)進(jìn)程數(shù)為 1 時 為 %(program_name)s 當(dāng)進(jìn)程數(shù) >1 時 應(yīng)配置為 %(program_name)s_%(process_num)02d
;numprocs=1 ; 進(jìn)程數(shù)量 (默認(rèn) 1)
;directory=/tmp ; 在執(zhí)行運行前切換到目錄 (def no cwd)
;umask=022 ; 掩碼 (default None)
;priority=999 ; 優(yōu)先級盹舞,數(shù)值越低越先啟動而越后關(guān)閉 (default 999)
;autostart=true ; 在 supervisord 啟動時即啟動 (default: true)
;startsecs=1 ; 需要考慮進(jìn)程啟動成功的時間, 當(dāng) running 狀態(tài)超過該值時产镐,表明啟動成功 (def. 1)
;startretries=3 ; 在啟動時狀態(tài)為失敗時的最大嘗試重啟次數(shù) (default 3)
;autorestart=unexpected ; 當(dāng)進(jìn)程退出時是否應(yīng)該重啟隘庄,可選值為 false true unexpected ,為 false 時表示不重啟癣亚,為 true 表示重啟丑掺,為 unexpected 時,如果退出狀態(tài)碼不是 exitcodes 中之一時進(jìn)行重啟 (def: unexpected)
;exitcodes=0,2 ; 用來重啟的狀態(tài)碼 (default 0,2)
;stopsignal=QUIT ; 進(jìn)程停止信號述雾,當(dāng)用設(shè)定的信號去干掉進(jìn)程街州,退出碼會被認(rèn)為是 expected (default TERM)
;stopwaitsecs=10 ; 這個是當(dāng)我們向子進(jìn)程發(fā)送 stopsignal 信號后,到系統(tǒng)返回信息給 supervisord玻孟,所等待的最大時間唆缴。超過這個時間,supervisord會向該子進(jìn)程發(fā)送一個強制 kill 的信號黍翎。 (default 10)
;stopasgroup=false ; 如果設(shè)置為 true 那么將會終止該進(jìn)程下的所有子進(jìn)程 (default false)
;killasgroup=false ; 當(dāng)進(jìn)程關(guān)閉時向該進(jìn)程的子進(jìn)程發(fā)送的是 kill 信號 (def false)
;user=chrism ; 可以管理該 program 的用戶
;redirect_stderr=true ; 如果為 true琐谤,那么 stderr 將會被寫入 stdout 日志文件中 (default false)
;stdout_logfile=/a/path ; 日志文件路徑, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; 日志文件的最大大小 (default 50MB)
;stdout_logfile_backups=10 ; 日志文件的最大數(shù)量 (default 10)
;stdout_capture_maxbytes=1MB ; capture 管道的大小玩敏,當(dāng)值不為 0 時斗忌,子進(jìn)程可以從 stdout 發(fā)送信息,而 supervisor 可以根據(jù)信息旺聚,發(fā)送相應(yīng)的 event (default 0)
;stdout_events_enabled=false ; 當(dāng)設(shè)置為 ture 的時候织阳,當(dāng)子進(jìn)程由 stdout 向文件描述符中寫日志的時候,將觸發(fā) supervisord 發(fā)送 PROCESS_LOG_STDOUT 類型的 event (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; 子進(jìn)程共享的環(huán)境變量 (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
常用配置可以像下面一樣:
[program:node]
command=node server.js
process_name=%(program_name)s
numprocs=1
directory=%(here)s/server
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=%(here)s/node-server.log
priority=999
啟動
配置完成之后使用 supervisord
命令來啟動服務(wù)端監(jiān)控:
supervisord -c /etc/supervisord.conf
如果不指定 -c
選項砰粹,那么會使用以下順序路徑進(jìn)行檢索 supervisord.conf
文件
- $CWD/supervisord.conf
- $CWD/etc/supervisord.conf
- /etc/supervisord.conf
- /etc/supervisor/supervisord.conf
- ../etc/supervisord.conf
- ../supervisord.conf
使用 supervisorctl 管理
啟動 supervisorctl 時需要指定配置文件路徑唧躲,否則它將和 supervisord 使用相同的配置文件檢索方式。
supervisorctl -c /etc/supervisord.conf
在 supervisorctl 的 shell 命令中可以使用以下命令:
> status # 查詢進(jìn)程狀態(tài)
> stop node # 關(guān)閉 [program:node] 的進(jìn)程
> start node # 啟動 [program:node] 的進(jìn)程
> restart node # 重啟 [program:node] 的進(jìn)程
> stop all # 關(guān)閉所有進(jìn)程
> start all # 啟動所有進(jìn)程
> reread # 重新讀取配置文件,讀取有更新(增加)的配置文件碱璃,不會啟動新添加的程序
> update # 重啟配置文件修改過的程序
除了在 shell 中使用命令弄痹,你也可以在命令行工具中直接輸入 supervisorctl 命令:
supervisorctl stop all
supervisorctl reload