supervisor安裝配置與使用

supervisor:C/S架構的進程控制系統,可使用戶在類UNIX系統中監控、管理進程。常用於管理與某個用戶或項目相關的進程。

組成部分
supervisord:服務守護進程
supervisorctl:命令行客戶端
Web Server:提供與supervisorctl功能相當的WEB操作界面
XML-RPC Interface:XML-RPC接口

安裝
centos平臺下可直接用過YUM源安裝
yum info supervisor
sudo yum install supervisor
sudo chkconfig supervisord on

服務器啓停
sudo /etc/init.d/supervisord {start|stop|status|restart|reload|force-reload|condrestart}

日誌
/var/log/supervisor/supervisord.log

配置文件
sudo vim /etc/supervisord.conf

需要重點關注的是以部分
[program:x]中配置要監控的進程


配置樣例

[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
;http_port=127.0.0.1:9001  ; (alternately, ip_address:port specifies AF_INET)
;sockchmod=0700              ; AF_UNIX socketmode (AF_INET ignore, default 0700)
;sockchown=nobody.nogroup     ; AF_UNIX socket uid.gid owner (AF_INET ignores)
;umask=022                   ; (process file creation umask;default 022)
logfile=/var/log/supervisor/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               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/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)

;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;http_username=user          ; (default is no username (open system))
;http_password=123           ; (default is no password (open system))
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;user=chrism                 ; (default is current user, required if root)
;directory=/tmp              ; (default is not to cd during start)
;environment=KEY=value       ; (key value pairs to add to environment)

[supervisorctl]
serverurl=unix:///var/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")

; 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:example]
;command=/bin/echo; the program (relative uses PATH, can take args)
;priority=999                ; the relative start priority (default 999)
;autostart=true              ; start at supervisord start (default: true)
;autorestart=true            ; retstart at unexpected quit (default: true)
;startsecs=10                ; number of secs prog must stay running (def. 10)
;startretries=3              ; max # of serial start failures (default 3)
;exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT             ; signal used to kill process (default TERM)
;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
;user=chrism                 ; setuid to this UNIX account to run the program
;log_stdout=true             ; if true, log program stdout (default true)
;log_stderr=true             ; if true, log program stderr (def false)
;logfile=/var/log/supervisor.log    ; child log path, use NONE for none; default AUTO
;logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
;logfile_backups=10          ; # of logfile backups (default 10)
“;”爲註釋。各參數的含義都很明確。可以根據官方手冊結合實驗來進一步深入瞭解。重點說幾個[program:example]中的參數
;command=/bin/echo; 		supervisor啓動時將要開啓的進程。相對或絕對路徑均可。若是相對路徑則會從supervisord的$PATH變中查找。命令可帶參數。
;priority=999                	指明進程啓動和關閉的順序。低優先級表明進程啓動時較先啓動關閉時較後關閉。高優先級表明進程啓動時啓動時較後啓動關閉時較先關閉。
;autostart=true              	是否隨supervisord啓動而啓動
;autorestart=true            	進程意外退出後是否自動重啓
;startsecs=10                	進程持續運行多久才認爲是啓動成功
;startretries=3              	重啓失敗的連續重試次數
;exitcodes=0,2               	若autostart設置爲unexpected且監控的進程並非因爲supervisord停止而退出,那麼如果進程的退出碼不在exitcode列表中supervisord將重啓進程
;stopsignal=QUIT             	殺進程的信號
;stopwaitsecs=10             	向進程發出stopsignal後等待OS向supervisord返回SIGCHILD 的時間。若超時則supervisord將使用SIGKILL殺進程
一個Rabbitmq項目中生產者和消費者進程使用supervisor監控的配置情況:(配置中的其他部分略)
</pre><p><pre name="code" class="plain">[program:worker_for_summary]
command=/home/op1/scripts/rabbitmqclient/worker_for_summary.py
priority=1
log_stderr=true             ; if true, log program stderr (def false)

[program:worker_for_detail_all]
command=/home/op1/scripts/rabbitmqclient/worker_for_detail_all.py
priority=1
log_stderr=true             ; if true, log program stderr (def false)

[program:worker_for_detail_recent_list]
command=/home/op1/scripts/rabbitmqclient/worker_for_detail_recent_list.py
priority=1
log_stderr=true             ; if true, log program stderr (def false)

[program:worker_for_detail_recent_sset]
command=/home/op1/scripts/rabbitmqclient/worker_for_detail_recent_sset.py
priority=1
log_stderr=true             ; if true, log program stderr (def false)

[program:publisher_for_summary]
command=/home/op1/scripts/rabbitmqclient/publisher_for_summary.py
priority=999
log_stderr=true             ; if true, log program stderr (def false)

[program:publisher_for_summary_nt]
command=/home/op1/scripts/rabbitmqclient/publisher_for_summary_nt.py
priority=999
log_stderr=true             ; if true, log program stderr (def false)

[program:publisher_for_detail]
command=/home/op1/scripts/rabbitmqclient/publisher_for_detail.py
priority=999
log_stderr=true             ; if true, log program stderr (def false)

[program:publisher_for_detail_nt]
command=/home/op1/scripts/rabbitmqclient/publisher_for_detail_nt.py
priority=999
log_stderr=true             ; if true, log program stderr (def false)
                                                                    

配置完成後啓動supervisord

sudo /etc/init.d/supervisord start
可以看到配置的各個進程在後臺運行了起來。
停掉某個進程後supervisor會馬上重啓該進程

停止supervisor
sudo /etc/init.d/supervisord stop
可以看到配置的各個進程都停止運行了。

可以通過supervisorctl查看管理監控的進程情況:

[op1@SVR1631HP360 ~]$ sudo supervisorctl
publisher_for_detail RUNNING    pid 27557, uptime 0:00:45
publisher_for_detail_nt RUNNING    pid 27567, uptime 0:00:45
publisher_for_summary RUNNING    pid 27566, uptime 0:00:45
publisher_for_summary_nt RUNNING    pid 27568, uptime 0:00:45
worker_for_detail_all RUNNING    pid 27581, uptime 0:00:45
worker_for_detail_recent RUNNING    pid 27582, uptime 0:00:45
worker_for_summary RUNNING    pid 27559, uptime 0:00:45

#可通過help瞭解命令的更多用法
supervisor> help

Documented commands (type help <topic>):
========================================
EOF    exit  maintail  quit    restart   start   stop
clear  help  open      reload  shutdown  status  tail

supervisor> help stop
stop <processname>			Stop a process.
stop <processname> <processname>	Stop multiple processes
stop all				Stop all processes
  When all processes are stopped, they are stopped in
  reverse priority order (see config file)
supervisor> help status
status			Get all process status info.
status <name>		Get status on a single process by name.
status <name> <name>	Get status on multiple named processes.

#停止某個進程
supervisor> stop publisher_for_summary
publisher_for_summary: stopped

#查看此時此刻的狀態
supervisor> status
publisher_for_detail RUNNING    pid 27557, uptime 0:05:41
publisher_for_detail_nt RUNNING    pid 27567, uptime 0:05:41
publisher_for_summary STOPPED    Feb 27 02:48 PM
publisher_for_summary_nt RUNNING    pid 27568, uptime 0:05:41
worker_for_detail_all RUNNING    pid 27581, uptime 0:05:41
worker_for_detail_recent RUNNING    pid 27582, uptime 0:05:41
worker_for_summary RUNNING    pid 27559, uptime 0:05:41
#發現被supervisorctl停掉的進程不會被自動重啓

#開啓剛纔停掉的進程
supervisor> start publisher_for_summary
publisher_for_summary: started
supervisor> status
publisher_for_detail RUNNING    pid 27557, uptime 0:08:02
publisher_for_detail_nt RUNNING    pid 27567, uptime 0:08:02
publisher_for_summary RUNNING    pid 3035, uptime 0:00:04
publisher_for_summary_nt RUNNING    pid 27568, uptime 0:08:02
worker_for_detail_all RUNNING    pid 27581, uptime 0:08:02
worker_for_detail_recent RUNNING    pid 27582, uptime 0:08:02
worker_for_summary RUNNING    pid 27559, uptime 0:08:02

#停掉所有進程
supervisor> stop all
worker_for_detail_recent: stopped
worker_for_detail_all: stopped
publisher_for_summary_nt: stopped
publisher_for_detail_nt: stopped
publisher_for_summary: stopped
worker_for_summary: stopped
publisher_for_detail: stopped
supervisor> status
publisher_for_detail STOPPED    Feb 27 02:51 PM
publisher_for_detail_nt STOPPED    Feb 27 02:51 PM
publisher_for_summary STOPPED    Feb 27 02:51 PM
publisher_for_summary_nt STOPPED    Feb 27 02:51 PM
worker_for_detail_all STOPPED    Feb 27 02:51 PM
worker_for_detail_recent STOPPED    Feb 27 02:51 PM
worker_for_summary STOPPED    Feb 27 02:51 PM

#開啓所有進程
supervisor> start all
publisher_for_detail: started
worker_for_summary: started
publisher_for_summary: started
publisher_for_detail_nt: started
publisher_for_summary_nt: started
worker_for_detail_all: started
worker_for_detail_recent: started
supervisor> status
publisher_for_detail RUNNING    pid 5111, uptime 0:00:15
publisher_for_detail_nt RUNNING    pid 5141, uptime 0:00:15
publisher_for_summary RUNNING    pid 5135, uptime 0:00:15
publisher_for_summary_nt RUNNING    pid 5147, uptime 0:00:15
worker_for_detail_all RUNNING    pid 5153, uptime 0:00:15
worker_for_detail_recent RUNNING    pid 5159, uptime 0:00:14
worker_for_summary RUNNING    pid 5112, uptime 0:00:15

更多內容請參考官方手冊
http://supervisord.org/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章