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序列呢?就是 , 这些。默认为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     查看服务状态(显示的类似于操作记录) 

原文地址:https://www.cnblogs.com/maociping/p/7161762.html