supervisor安裝及其配置


一、supervisor概述       

    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頁面來查看和管理進程狀態。

二、supervisor安裝及相關配置

(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的配置參數較多,詳細的配置參數說明請參考官方文檔介紹,下面介紹一些常用的參數配置,分號(;)開頭的配置表示注釋。

[unix_http_server]
file=/tmp/supervisor.sock ; socket文件的路徑,supervisorctl基於它通過XML_RPC和supervisord通信。如果不設置,
則supervisorctl不能用,默認為none。可修改該文件的路徑,例如/var/run/supervisor.sock,非必須設置項
;chmod=0700 ; 上述socket文件的權限值,如果不設置,默認為0700。非必須設置項
;chown=nobody:nogroup ; 上述socket文件所屬的用戶:組,如果不設置,默認為啟動supervisord進程的用戶及屬組。非必須設置項
;username=user ; supervisorctl連接時,認證的用戶名,如果不設置,默認不需要認證。非必須設置項
;password=123 ; 上述認證用戶名對應的密碼,可以直接使用明碼,也可以使用SHA加密,
如:{SHA}82ab876d1387bfafe46cc1c8a2ef074eae50cb1d
默認不需要設置,與上述username成對出現。非必須設置項

;[inet_http_server] ; 偵聽在TCP上的socket,Web Server和遠程的supervisorctl都要用到它,如果不設置,默認為不開啟。非必須設置項
;port=127.0.0.1:9001 ; 偵聽的IP和端口,偵聽9001端口的所有IP":9001或*:9001"
如果開啟了[inet_http_server],則必須設置該項
;username=user ; 認證的用戶名,默認不設置。非必須設置項
;password=123 ; 認證用戶對應的認證密碼,與認證用戶名成對出現。非必須設置項

[supervisord] ;主要定義服務端進程supervisord的相關屬性。必須設置項
logfile=/tmp/supervisord.log ; supervisord主進程的日志路徑,注意和子進程日志區別。
默認路徑$CWD/supervisord.log,$CWD是當前目錄。非必須設置項
logfile_maxbytes=50MB ; 日志文件的最大大小,當超過50M的時候,會生成一個新的日志文件。當設置為0時,表示不限制文件大小,默認值是50M。非必須設置項
logfile_backups=10 ; 日志文件保留備份的數量,supervisor在啟動程序時,會自動創建10個buckup文件,用於log rotate,當設置為0時表示不備份,
默認值為10。非必須設置項
loglevel=info ; 日志級別,有critical, error, warn, info, debug, trace, or blather等,默認為info。非必須設置項
pidfile=/tmp/supervisord.pid ; supervisord的pid文件路徑,默認為$CWD/supervisord.pid。非必須設置
nodaemon=false ; 如果為true,supervisord進程將在前台運行,默認為false,即以守護進程在后台運行。非必須設置項
minfds=1024 ; 最少系統空閑的文件描述符,低於該值supervisor將不會啟動。系統的文件描述符在/proc/sys/fs/file-max設置,
默認值為1024。非必須設置項
minprocs=200 ; 最少可用的進程描述符,低於該值supervisor將不會正常啟動。利用"ulimit -u"命令可以查看linux下用戶的最大進程數,
默認值為200。非必須設置項
;umask=022 ; 進程創建文件的掩碼,默認為022。非必須設置項
;user=chrism ; 設置一個非root用戶,當以root用戶啟動supervisord之后,設置的該用戶也可以對supervisord進行管理,
默認為不設置。非必須設置項
;identifier=supervisor ; supervisord的標識符,主要是XML_RPC調用時標識supervisor。當有多個supervisor的時候,而且想調用XML_RPC統一管理,就需要為每個
supervisor設置不同的標識符了,默認是supervisord。非必需設置項
;directory=/tmp ; 如果設置該參數,則當supervisord作為守護進程運行前,會先切換到該目錄,默認不設置。非必須設置項
;nocleanup=true ; 該參數值為false時,則supervisord進程啟動時,會將以前子進程
產生的日志文件(路徑為AUTO的情況下)清除掉。當需要看歷史日志,則設置為true,默認為false,
調試時可以設置為true。非必須設置項
;childlogdir=/tmp ; 當子進程日志路徑為AUTO時,子進程日志文件的存放路徑。
默認路徑是這個東西,執行下面的這個命令看看就OK了,處理的東西就默認路徑
python -c "import tempfile;print tempfile.gettempdir()"。非必須設置項
;environment=KEY="value" ; 設置環境變量,supervisord在linux中啟動默認繼承linux的環境變量,該參數可設置supervisord進程特有的環境變量。
supervisord啟動子進程時,子進程會拷貝父進程的內存空間內容。所以設置的環境變量也會被子進程繼承。
小例子:environment=name="hello",age="18",默認為不設置。非必須設置項
;strip_ansi=false ; 如果設置為true,則會清除子進程日志中所有的ANSI序列。什么是ANSI序列呢?就是\n,\t這些。默認為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] ;該參數為XML_RPC服務,如果使用supervisord或者web server,該選項必須要開啟
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl] ;主要針對supervisorctl的一些屬性配置
serverurl=unix:///tmp/supervisor.sock ; 該參數為本地UNIX socket路徑,當supervisorctl本地連接supervisord時需用到,這個是和前面的[unix_http_server]相對應,
默認值就是unix:///tmp/supervisor.sock。非必須設置項
;serverurl=http://127.0.0.1:9001 ; 該參數為supervisorctl遠程連接supervisord時,用到的TCP socket路徑,其與前面的[inet_http_server]相對應,
默認為http://127.0.0.1:9001。非必須設置項

;username=chris ; 連接時用戶名,默認為空。非必須設置項
;password=123 ; 連接時密碼,默認為空。非必須設置項
;prompt=mysupervisor ; 輸入用戶名密碼時的提示符,默認為supervisor。非必須設置項
;history_file=~/.sc_history ; 該參數與shell中的history類似,上下鍵查看執行過的歷史命令,默認是沒有指定文件存儲的。如需該功能,須指定一個文件。非必須設置項

; 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] ; 管理的子進程,":"后面是子進程名字,最好和實際進程相關聯。program可以設置一個或多個,一個program就是一個要被管理的進程
;command=/bin/cat ; 被管理進程啟動的命令絕對路徑,可以帶參數,例如:/home/hello.py 8080
需要注意的是,command只能是那種在終端運行的進程,不能是守護進程。比如說command=service httpd start,httpd是守護進程,
它已經被linux的service(CentOS7是systemctl)管理了,如果再去用supervisor啟動該進程,那么它已經不是嚴格意義上的子進程了。
必須設置項
;process_name=%(program_name)s ; 進程名,如果下面的numprocs參數為1,則不用管該參數,它默認值為%(program_name)s,即program冒號后的theprogramname,
但如果numprocs>1,就需要為每個進程取個名字了,否則每個進程都用同一個進程名。
;numprocs=1 ; 啟動的進程數。當大於1時,就是進程池的概念,此時需要注意process_name的設置,默認為1。非必須設置項
;directory=/tmp ; 進程運行前,會切換到該目錄,默認不設置。非必須設置項
;umask=022 ; 進程掩碼,默認為none,非必須設置項
;priority=999 ; 子進程啟動關閉優先級,優先級值越低,最先啟動,關閉的時候最后關閉,默認值為999。非必須設置項
;autostart=true ; 如果為true,子進程將在supervisord啟動后被自動啟動,默認為true。非必須設置項
;autorestart=unexpected ; 設置子進程掛掉后自動重啟的情況,有三個選項,false,unexpected和true。如果為false,無論什么情況,都不會被重新啟動,
如果為unexpected,只有當進程的退出碼不在下面的exitcodes里面定義的退出碼時,才會被自動重啟。
如果為true,只要子進程掛掉,將會被無條件的重啟。
;startsecs=1 ; 該選項是子進程啟動多少秒后,此時狀態如果為running,則認為啟動成功了,默認值為1。非必須設置項
;startretries=3 ; 當進程啟動失敗后,最大嘗試啟動的次數。當超過3次后,supervisor將把此進程的狀態置為FAIL,默認值為3 。非必須設置項
;exitcodes=0,2 ; 與上面的autorestart=unexpected對應。exitcodes里定義的退出碼是expected執行的條件。
;stopsignal=QUIT ; 進程停止信號,可以為TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信號
默認為TERM。當用設定的信號去kill進程,退出碼會被認為是expected。非必須設置項
;stopwaitsecs=10 ; 當向子進程發送stopsignal信號后,到系統返回信息給supervisord所等待的最大時間。超過該時間,supervisord會向該
子進程發送一個強制kill的信號,默認為10秒。非必須設置項
;stopasgroup=false ; 用於supervisord管理的子進程,該子進程本身還有
子進程的情況。如果僅僅kill掉supervisord子進程,那么該子進程的子進程有可能會變成孤兒進程。設置該選項,
則可以把該子進程的整個進程組都干掉。 設置為true的話,一般killasgroup也會被設置為true。
需要注意的是,該選項發送的是stop信號,默認為false。非必須設置項
;killasgroup=false ; 與上面的stopasgroup類似,不過發送的是kill信號
;user=chrism ; 如果supervisord是root啟動,在這里可設置非root用戶,那么該用戶可用來管理該program,默認不設置。非必須設置項項
;redirect_stderr=true ; 如果為true,則stderr的日志會被寫入stdout日志文件中,默認為false。非必須設置項
;stdout_logfile=/a/path ; 子進程stdout的日志路徑,可以指定路徑,AUTO,none等三個選項。設置為none,將沒有日志產生。設置為AUTO,將會隨機找個路徑
生成日志文件,且當supervisord重新啟動時,以前的日志文件會被清空。當redirect_stderr=true時,sterr也會寫進這個日志文件。
;stdout_logfile_maxbytes=1MB ; 日志文件最大大小,與[supervisord]中定義的一樣。默認為50
;stdout_logfile_backups=10 ; 與[supervisord]定義的一樣。默認10
;stdout_capture_maxbytes=1MB ; 設定capture管道的大小,當值不為0,子進程可以從stdout
發送信息,而supervisor可以根據信息,發送相應的event。默認為0,為0時表示關閉管道。非必須設置項
;stdout_events_enabled=false ; 當設置為ture,當子進程由stdout向文件描述符中寫日志時,將
觸發supervisord發送PROCESS_LOG_STDOUT類型的event,默認為false。非必須設置項
;stderr_logfile=/a/path ; 設置stderr的日志路徑,當redirect_stderr=true,該項就不用設置了,設置了也不會生效。因為它會被寫入stdout_logfile的同一個文件中
默認為AUTO,就是會隨機找個路徑存儲,supervisord重啟被清空。非必須設置項
;stderr_logfile_maxbytes=1MB ; 設置stderr文件最大大小
;stderr_logfile_backups=10 ; 設置stderr文件的備份副本個數
;stderr_capture_maxbytes=1MB ; 與stdout_capture一樣。默認為0,關閉狀態。
;stderr_events_enabled=false ; 與stdout_events_enabled項類似,默認為false
;environment=A="1",B="2" ; 該子進程的環境變量,與其它子進程不共享
;serverurl=AUTO ;

; 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] ;與program功能類似,也是suopervisor啟動的子進程,不過它是訂閱supervisord發送的event。它的名字就叫
listener了。我們可以在listener中做一系列處理,比如報警等。
;command=/bin/eventlistener ; 與上述program一樣,表示listener可執行文件的路徑
;process_name=%(program_name)s ; 進程名,當numprocs>1時,才需要設置。否則就用默認值
;numprocs=1 ; 相同listener啟動的個數
;events=EVENT ; event事件的類型,只有寫在這個地方的事件類型才會被發送。
;buffer_size=10 ; event隊列緩存大小,單位需要確認。當buffer超過10時,最舊的event將會被清除,並把新的event放進去。默認值為10。非必須設置項
;directory=/tmp ; 進程執行前,會切換到該目錄,默認為不切換。非必須設置項
;umask=022 ; 掩碼,默認為none
;priority=-1 ; 啟動優先級,默認-1
;autostart=true ; 是否隨supervisord啟動一起啟動,默認true
;autorestart=unexpected ; 是否自動重啟,與program一樣,分true,false,unexpected等,注意unexpected和exitcodes的關系
;startsecs=1 ; 進程啟動后運行多久才被認定為成功啟動,默認1
;startretries=3 ; 失敗最大嘗試次數,默認3
;exitcodes=0,2 ; unexpected中的進程退出碼
;stopsignal=QUIT ; kill進程的信號,默認為TERM,比如設置為QUIT,那么如果QUIT來kill該進程,那么會被認為是正常維護,退出碼也被認為是expected中的
;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 ; 設置普通用戶來管理該listener進程,默認為空。非必須設置項
;redirect_stderr=true ; 為true的話,stderr的log會並入stdout的log里面,默認為false。非必須設置項
;stdout_logfile=/a/path ; 與上述類似
;stdout_logfile_maxbytes=1MB ; 與上述類似
;stdout_logfile_backups=10 ; 與上述類似
;stdout_events_enabled=false ; 這個其實是錯的,listener是不能發送event
;stderr_logfile=/a/path ; 與上述類似
;stderr_logfile_maxbytes=1MB ; 與上述類似
;stderr_logfile_backups ; 與上述類似
;stderr_events_enabled=false ; 這個也是錯的,listener不能發送event
;environment=A="1",B="2" ; 該子進程的環境變量,默認為空。非必須設置項
;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分組,划分到組里面的program。設置后就不用一個一個去操作了我們可以對組名進行統一的操作。
注意:program被划分到組之后,就相當於原來的配置從supervisor的配置文件里消失了。
supervisor只會對組進行管理,而不再會對組里面的單個program進行管理了
;programs=progname1,progname2 ; 組成員,用逗號分開,必須設置項
;priority=999 ; 優先級,相對於組和組之間,默認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] ; 有用的配置項,當管理的進程很多時,寫一個配置文件就會很多,不夠清晰。
那么設置該項就可以把配置信息寫到多個文件中,然后include過來就可以了。
;files = relative/directory/*.ini ; 可以指定一個或多個以.ini或.conf為后綴的配置文件

include示例:
[include]
files = /opt/absolute/filename.ini /opt/absolute/*.ini foo.conf config??.ini

三、配置管理進程

進程管理配置參數,不建議全都寫在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
/etc/supervisord.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
supervisor_tornado.conf

四、supervisor相關命令

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 重新加載配置文件

五、把supervisor加入開機自啟動服務(CentOS7.X系統)

(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管理了,后續起停supervisor服務都可以通過systemctl來控制了

   systemctl start supervisor.service      啟動服務

   systemctl stop supervisor.service       停止服務

   systemctl restart supervisor.service    重新啟動服務

   systemctl reload supervisor.service     重載配置文件

   systemctl status supervisor.service     查看服務狀態(顯示的類似於操作記錄) 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM