supervisor是一個c/s系統,被用來在類Unix系統中監控進程狀態。supervisor使用python開發。 服務端進程爲supervisord,主要負責啓動自身及其監控的子進程,響應客戶端命令,重啓異常退出的子進程,記錄子進程stdout和stderr輸出,生成和處理子進程生命週期中的事件。其配置文件通常爲/etc/supervisord.conf,能夠在配置文件中配置相關參數,包括supervisord自身的狀態,其管理的各個子進程的相關屬性等。supervisor的客戶端爲supervisorctl,它提供了一個類shell的接口(即命令行)來操做supervisord服務端。經過supervisorctl,能夠鏈接到supervisord服務進程,得到服務進程監控的子進程狀態,啓動和中止子進程,得到正在運行的進程列表。客戶端經過Unix域套接字或者TCP套接字與服務進程進行通訊,服務器端具備身份憑證認證機制,能夠有效提高安全性。當客戶端和服務端位於同一臺機器上時,客戶端與服務器共用同一個配置文件/etc/supervisord.conf,經過不一樣標籤來區分二者的配置。supervisor也提供了一個web頁面來查看和管理進程狀態。html
(1)安裝
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz#md5=04766d62864da13d6a12f7429e75314f
tar zxvf supervisor-3.3.2.tar.gz && cd supervisor-3.3.2
python setup.py install
supervisor安裝完成後會生成三個執行程序:supervisortd、supervisorctl以及echo_supervisord_conf,它們分別是supervisor的守護進程服務(用於接收進程管理命令)、
客戶端(用於和守護進程通訊,發送管理進程的指令)以及生成初始配置文件程序。
(2)配置
運行supervisord服務的時候,須要指定supervisor配置文件,若是沒有顯示指定,默認在如下目錄或文件中查找(其中$CWD表示運行supervisord程序的目錄):
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
../etc/supervisord.conf (Relative to the executable)
../supervisord.conf (Relative to the executable)
安裝完成後,能夠經過運行echo_supervisord_conf程序生成supervisor的初始化配置文件,以下所示:
echo_supervisord_conf > /etc/supervisord.conf 生成supervisor的主配置文件
mkdir /etc/supervisord.d 用戶存放被監控進程的配置文件
(3)配置文件參數說明
supervisor的配置參數較多,詳細的配置參數說明請參考官方文檔介紹,下面介紹一些經常使用的參數配置,分號(;)開頭的配置表示註釋。python
[unix_http_server] ;[inet_http_server] ; 偵聽在TCP上的socket,Web Server和遠程的supervisorctl都要用到它,若是不設置,默認爲不開啓。非必須設置項 [supervisord] ;主要定義服務端進程supervisord的相關屬性。必須設置項 ; the below section must remain in the config file for RPC [rpcinterface:supervisor] ;該參數爲XML_RPC服務,若是使用supervisord或者web server,該選項必需要開啓 [supervisorctl] ;主要針對supervisorctl的一些屬性配置 ; The below sample program section shows all possible program subsection values, ;[program:theprogramname] ; 管理的子進程,":"後面是子進程名字,最好和實際進程相關聯。program能夠設置一個或多個,一個program就是一個要被管理的進程 ; The below sample eventlistener section shows all possible ;[eventlistener:theeventlistenername] ;與program功能相似,也是suopervisor啓動的子進程,不過它是訂閱supervisord發送的event。它的名字就叫 ; The below sample group section shows all possible group values, ;[group:thegroupname] ; 給programs分組,劃分到組裏面的program。設置後就不用一個一個去操做了咱們能夠對組名進行統一的操做。 ; The [include] section can just contain the "files" setting. This ;[include] ; 有用的配置項,當管理的進程不少時,寫一個配置文件就會不少,不夠清晰。 include示例: |
進程管理配置參數,不建議全都寫在supervisord.conf文件中,應該每一個進程寫一個配置文件放在include指定的目錄下,幷包含進supervisord.conf文件中。
建立/etc/supervisord.d目錄,用於存放進程管理的配置文件
修改/etc/supervisord.conf中的include參數,將/etc/supervisor.d目錄添加到include中,實例以下
; 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". ; - Quotes around values are not supported, except in the case of ; the environment= options as shown below. ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". ; - Command will be truncated if it looks like a config file comment, e.g. ; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ". [unix_http_server] file=/var/run/supervisor.sock ; the path to the socket file #chmod=0777 ; 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=*: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=/var/log/supervisord.log ; main log file; default $CWD/supervisord.log logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace 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 ;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 rpcinterface:supervisor 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:x] sections. [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface ; The supervisorctl section configures how supervisorctl will connect to ; supervisord. configure it match the settings in either the unix_http_server ; or inet_http_server section. [supervisorctl] serverurl=unix:///var/run/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 in [*_http_server] if set ;password=123 ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available ; The sample program section below 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 (0 means none, 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 (0 means none, 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 sample eventlistener section below shows all possible eventlistener ; subsection values. Create one or more 'real' eventlistener: sections to be ; able to handle event notifications sent by supervisord. ;[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 (0 means none, 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 (0 means none, 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 sample group section below 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 = /etc/supervisord.d/*.conf
#爲了方便管理,增長一個tornado組 [group:tornados] programs=tornado-0,tornado-1 # 分別定義兩個tornado的進程配置 [program:tornado-0] # 進程要執行的命令 #command=python /home/mcp/tornado/hello.py --port=8000 command=python /home/mcp/tornado/hello.py 8000 directory=/home/mcp/tornado/ user=mcp autostart=true # 自動重啓 autorestart=true redirect_stderr=true # 日誌路徑 stdout_logfile=/home/mcp/tornado/tornado0.log loglevel=info [program:tornado-1] #command=python /home/mcp/tornado/hello.py --port=8001 command=python /home/mcp/tornado/hello.py 8001 directory=/home/mcp/tornado/ user=mcp autostart=true autorestart=true redirect_stderr=true stdout_logfile=/home/mcp/tornado/tornado1.log loglevel=info
supervisord相關命令:
supervisord 啓動服務端進程
/usr/bin/supervisord -c /etc/supervisord.conf 按指定配置文件啓動服務端進程
supervisorctl相關命令:
supervisorctl 進入交互界面
supervisorctl status 查看被監控進程狀態
supervisorctl stop all 關閉被監控的進程
supervisorctl start all 啓動被監控的進程
supervisorctl start program-name 其中program-name爲配置文件[program:xx]中的xx
supervisorctl stop program-name 其中program-name爲配置文件[program:xx]中的xx
supervisorctl restart all 重啓被監控的進程
supervisorctl reatart program-name 重啓某一進程,program-name爲[program:xx]中的xx
supervisorctl shutdown 關閉supervisord服務端
supervisorctl reload 從新加載配置文件
(1)利用/etc/rc.local
echo "/usr/bin/supervisord -c /etc/supervisord.conf" >> /etc/rc.local
/etc/rc.local -> rc.d/rc.local /etc/rc.local是/etc/rc.d/rc.local的軟鏈接
若是開機啓動不生效,則首先須要檢查下/etc/rc.d/rc.local是否具備可執行權限
(2)加入systemctl管理
vim /lib/systemd/system/supervisor.service
[Unit] Description=supervisor After=network.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown ExecReload=/usr/bin/supervisorctl $OPTIONS reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
上述文件編寫後,執行以下命令便可:
systemctl enable supervisor.service 加入開機自啓動服務
systemctl daemon-reload 從新載入systemd,掃描新的或有變更的單元(必要步驟)
chmod 766 /lib/systemd/system/supervisor.service 修改文件權限
經過上述(五),實際上supervisor已經加入了systemctl管理了,後續起停supervisor服務均可以經過systemctl來控制了
systemctl start supervisor.service 啓動服務
systemctl stop supervisor.service 中止服務
systemctl restart supervisor.service 從新啓動服務
systemctl reload supervisor.service 重載配置文件
systemctl status supervisor.service 查看服務狀態(顯示的相似於操做記錄)