Supervisor (进程管理利器) 使用说明 - 运维笔记

 

一、Supervisor简单介绍
supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序。supervisor与launchd,daemontools,runit等程序有着相同的功能,与其中某些程序不同的是,它并不作为“id 为 1的进程”而替代init。相反,它用于控制应用程序,像启动其它程序一样,通俗理解就是,把Supervisor服务管理的进程程序,它们作为supervisor的子进程来运行,而supervisor是父进程。supervisor来监控管理子进程的启动关闭和异常退出后的自动启动。

至于为什么要用supervisor来管理进程,是因为相对于linux传统的进程管理(即系统自带的init 进程管理)方式来说,它有很多的优势:

1) 简单方便
通常管理linux进程的时候,一般来说都需要自己编写一个能够实现进程start/stop/restart/reload功能的脚本,然后丢到/etc/init.d/下面。其实这么做有很多不好的地方:
a) 编写这个脚本,耗时耗力。
b) 当这个进程挂掉的时候,linux不会自动重启它的,想要自动重启的话,还要自己另外写一个监控重启脚本。

supervisor则可以完美的解决上面这那两个问题! 那么supervisor怎么解决呢?
a) supervisor管理进程,就是通过fork/exec的方式把这些被管理的进程,当作supervisor的子进程来启动。这样的话,只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去就OK了。这样就省下了自己写脚本管理linux进程的麻烦了。
b) 被管理进程作为supervisor的子进程,当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,所以也就可以对挂掉的子进程进行自动重启了, 至于重启还是不重启,也要看配置文件里面有没有设置autostart=true。

2) 精确
linux对进程状态的反馈有时候不太准确, 也就是说linux进程通常很难获得准确的up/down状态, Pidfiles经常说谎!  而supervisor监控子进程,得到的子进程状态无疑是准确的。supervisord将进程作为子进程启动,所以它总是知道其子进程的正确的up/down状态,可以方便的对这些数据进行查询. 

3) 进程分组
进程支持分组启动和停止,也支持启动顺序,即‘优先级’,supervisor允许为进程分配优先级,并允许用户通过supervisorctl客户端发出命令,如“全部启动”和”重新启动所有“,它们以预先分配的优先级顺序启动。还可以将进程分为”进程组“,一组逻辑关联的进程可以作为一个单元停止或启动。进程组supervisor可以对进程组统一管理,也就是说我们可以把需要管理的进程写到一个组里面,然后把这个组作为一个对象进行管理,如启动,停止,重启等等操作。而linux系统则是没有这种功能的,想要停止一个进程,只能一个一个的去停止,要么就自己写个脚本去批量停止。

4) 集中式管理
supervisor管理的进程,进程组信息,全部都写在一个ini格式的文件里就OK了。管理supervisor时, 可以在本地进行管理,也可以远程管理,而且supervisor提供了一个web界面,可以在web界面上监控,管理进程。 当然了,本地,远程和web管理的时候,需要调用supervisor的xml_rpc接口。

5) 可扩展性
supervisor有一个简单的事件(event)通知协议,还有一个用于控制的XML-RPC接口,可以用Python开发人员来扩展构建。

6) 权限
总所周知, linux的进程特别是侦听在1024端口之下的进程,一般用户大多数情况下,是不能对其进行控制的。想要控制的话,必须要有root权限。然而supervisor提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程了。

7) 兼容性,稳定性
supervisor由Python编写,在除Windows操作系统以外基本都支持,如linux,Mac OS x,solaris,FreeBSD系统

二、Supervisor组成部分
1)supervisord: 服务守护进程
supervisor服务器的进程名是supervisord。它主要负责在自己的调用中启动子程序,响应客户端的命令,重新启动崩溃或退出的进程,记录其子进程stdout和stderr的输出,以及生成和处理对应于子进程生命周期中的"event"服务器进程使用的配置文件,通常路径存放在/etc/supervisord.confa中。此配置文件是INI格式的配置文件。
2) supervisorctl:命令行客户端
supervisor命令行的客户端名称是supervisorctl。它为supervisord提供了一个类似于shell的交互界面。使用supervisorctl,用户可以查看不同的supervisord进程列表,获取控制子进程的状态,如停止和启动子进程
3) Web Server:提供与supervisorctl功能相当的WEB操作界面
一个可以通过Web界面来查看和控制进程的状态,默认监听在9091上。
4) XML-RPC Interface:XML-RPC接口
supervisor用于控制的XML-RPC接口

三、Supervisor安装 (YUM安装)
centos系统下可以直接yum安装, 前提是需要下载epel源, 下载地址: http://dl.fedoraproject.org/pub/epel/

==================centos6版本系统==================
[root@localhost ~]# rpm -ivh epel-release-latest-6.noarch.rpm --force
[root@localhost ~]# yum install -y supervisor 

开机启动
[root@localhost ~]# chkconfig supervisord on

启动/关闭/重启等操作
[root@localhost ~]# /etc/init.d/supervisord {start|stop|status|restart|reload|force-reload|condrestart}

==================centos7版本系统==================
[root@localhost ~]# rpm -ivh epel-release-latest-7.noarch.rpm --force
[root@localhost ~]# yum install -7 supervisor 

开机启动
[root@localhost ~]# systemctl enable supervisord

启动/关闭/重启等操作
[root@localhost ~]# systemctl start/stop/restart supervisord

特别注意另一种安装方式: 除了yum安装方式以外, 我们通过会选用pip或easy_install方式安装supervisor.  但是supervisor目前只有python2支持的版本, 目前不支持python3.

centos7下安装
[root@localhost ~]# yum install -y python-setuptools
[root@localhost ~]# easy_install supervisor    或者 "pip install supervisor"

配置文件, 将默认配置保存在/etc/supervisord.conf中
[root@localhost ~]# echo_supervisord_conf > /etc/supervisord.conf      

启动
[root@localhost ~]# supervisord -c /etc/supervisord.conf
[root@localhost ~]# ps -ef|grep /etc/supervisord
root     26586     1  0 02:02 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     26588 26184  0 02:02 pts/0    00:00:00 grep --color=auto /etc/supervisord

[root@localhost ~]# which supervisord
/usr/bin/supervisord
[root@localhost ~]# which supervisorctl
/usr/bin/supervisorctl

如果没有下面文件, 就手动创建
[root@localhost ~]# vim /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target
   
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecReload=/usr/bin/supervisorctl reload    
ExecStop=/usr/bin/supervisorctl shutdown   
   
[Install]
WantedBy=multi-user.target

设置755权限
[root@localhost ~]# chmod 755 /usr/lib/systemd/system/supervisord.service

重启服务(多测试几次)
[root@localhost ~]# ps -ef|grep /etc/supervisord
root     26586     1  0 02:02 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     26933 26184  0 02:08 pts/0    00:00:00 grep --color=auto /etc/supervisord
[root@localhost ~]# /usr/bin/supervisorctl shutdown
Shut down
[root@localhost ~]# ps -ef|grep /etc/supervisord                   
root     26940 26184  0 02:08 pts/0    00:00:00 grep --color=auto /etc/supervisord

由于上面的supervisord进程是使用配置文件手动启动的, 首次要使用下面的命令关闭,然后使用"systemctl start/stop supervisord" 才会生效!
如果第一次不使用下面命令关闭, 而首次就使用"systemctl stop supervisord" 则关闭不了.
[root@localhost ~]# systemctl start supervisord
[root@localhost ~]# ps -ef|grep /etc/supervisord
root     27049     1  0 02:09 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     27052 26184  0 02:09 pts/0    00:00:00 grep --color=auto /etc/supervisord

[root@localhost ~]# systemctl stop supervisord  
[root@localhost ~]# ps -ef|grep /etc/supervisord
root     27068 26184  0 02:09 pts/0    00:00:00 grep --color=auto /etc/supervisord

[root@localhost ~]# systemctl start supervisord 
[root@localhost ~]# systemctl restart supervisord
[root@localhost ~]# systemctl reload supervisord
[root@localhost ~]# ps -ef|grep /etc/supervisord 
root     27097     1  0 02:09 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     27100 26184  0 02:09 pts/0    00:00:00 grep --color=auto /etc/supervisord

开机启动
[root@localhost ~]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

如下简单配置实例
[root@localhost ~]# vim /etc/supervisord.conf
.......

#  不采用sock连接方式
;[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)

#采用http连接方式, 并设置登录验证信息
[inet_http_server]         ; inet (TCP) server disabled by default
port=192.168.10.10: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)

# 添加被监控程序,被监控程序自身非后台运行
[program:nginx]
command=/usr/local/nginx/sbin/nginx
process_name=nginx
autostart=true
autorestart=true

使配置生效 
[root@localhost ~]# systemctl reload supervisord

需要注意的地方
1) 如果autostart设为true(默认),而被监控进程为单例模式运行,则在被监控进程已运行的情况下重启supervisord,
    supervisord会不停的尝试启动被监控进程,造成资源浪费。
2) 被监控进程的stdout和stderr输出默认被保存在单独的临时文件中,可根据需要进行配置。supervisord默认启动时会清除这些临时文件,
    如需要可修改配置nocleanup=true。建议将被监控进程的输出手动重定向。

运行
[root@localhost ~]# systemctl start supervisord

supervisor日志文件: /var/log/supervisor/supervisord.log
supervisor配置文件: /etc/supervisord.conf
supervisor连接方式: http和sock 两种

supervisor监控管理

1) web界面管理
为了更方便的远程管理 Supervisor ,我们还可以开启其自带的 web 控制台。打开 /supervisord.conf ,在文件末尾增加以下内容:
[root@kevin ~]# vim /etc/supervisord.conf
......
[inet_http_server]
port=*:5000
username=user
password=123456

配置说明:
port 为监听端口,username 和 password 分别为帐号和密码。
保存文件后需要重启supervisor
然后访问"http://ip:5000" , 输入上面设置的用户名和密码即可打开supervisor的web 控制台了。

2) 客户端命令行操作
[root@170~ ~]# supervisorctl -s http://ip:port -u user -p passwd command

3) 通过shell来管理supervisor ,可以使用以下命令:
supervisorctl start appname      #启动特定程序
supervisorctl stop appname       #停止特定程序
supervisorctl restart appname    #重启特定程序
supervisorctl start all      #启动所有程序
supervisorctl stop all       #停止所有程序
supervisorctl restart all    #重启所有程序

最后需要注意的是:
如果使用 Supervisor 监控 shell 脚本,不能在脚本中完全使用 nohup, setsid 等后台运行命令,否则 supervisor 会误认为程序自动退出而不断重启脚本。

 

Supervisord安装完成后有两个可用的命令行: supervisor和supervisorctl

常见的命令如下:
supervisord        初始启动Supervisord,启动、管理配置中设置的进程
supervisorctl stop programxxx        停止某一个进程(programxxx),programxxx为[program:chatdemon]里配置的值,这个示例就是chatdemon
supervisorctl start programxxx        启动某个进程
supervisorctl restart programxxx     重启某个进程
supervisorctl stop groupworker       重启所有属于名为groupworker这个分组的进程(start,restart同理)
supervisorctl stop all     停止全部进程,注:start、restart、stop都不会载入最新的配置文件
supervisorctl reload      载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl update     根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。注意:显示用stop停止掉的进程,用reload或者update都不会自动重启

常见命令
supervisorctl tail programname       //查看programname的日志
supervisorctl tail redis        //查看日志

supervisor事件监听及通知机制
-  supervisor向listeners发送和子进程或自身有关的notification。对于同一pool内的listeners,supervisor会选取任一可用的进行通知。
-  配置被监控进程[program:x]的日志Capture Mode,被监控进程可向stdout输出业务数据,由supervisod捕获这些数据,发给listener。
-  配置event-listener:监听PROCESS_COMMUNICATION_STDOUT事件
-  envent-listener模块开发:使用python的supervisor.childutils模块。该模块可作为监控代理模块,和进程及网管服务通信。与网管服务可采用redis的list实现。

四、Supervisor配置文件说明   (其中[program:x]中配置要监控的进程 ) 

[unix_http_server]           
file=/tmp/supervisor.sock   ; socket文件的路径,supervisorctl用XML_RPC和supervisord通信就是通过它进行
                              的。如果不设置的话,supervisorctl也就不能用了 
                              不设置的话,默认为none。 非必须设置       
;chmod=0700                 ; 这个简单,就是修改上面的那个socket文件的权限为0700
                              不设置的话,默认为0700。 非必须设置
;chown=nobody:nogroup       ; 这个一样,修改上面的那个socket文件的属组为user.group
                              不设置的话,默认为启动supervisord进程的用户及属组。非必须设置
;username=user              ; 使用supervisorctl连接的时候,认证的用户
                               不设置的话,默认为不需要用户。 非必须设置
;password=123               ; 和上面的用户名对应的密码,可以直接使用明码,也可以使用SHA加密
                              如:{SHA}82ab876d1387bfafe46cc1c8a2ef074eae50cb1d
                              默认不设置。。。非必须设置
 
;[inet_http_server]         ; 侦听在TCP上的socket,Web Server和远程的supervisorctl都要用到他
                              不设置的话,默认为不开启。非必须设置
;port=127.0.0.1:9001        ; 这个是侦听的IP和端口,侦听所有IP用 :9001或*:9001。
                              这个必须设置,只要上面的[inet_http_server]开启了,就必须设置它
;username=user              ; 这个和上面的uinx_http_server一个样。非必须设置
;password=123               ; 这个也一个样。非必须设置
 
[supervisord]                ;这个主要是定义supervisord这个服务端进程的一些参数的
                              这个必须设置,不设置,supervisor就不用干活了
logfile=/tmp/supervisord.log ; 这个是supervisord这个主进程的日志路径,注意和子进程的日志不搭嘎。
                               默认路径$CWD/supervisord.log,$CWD是当前目录。。非必须设置
logfile_maxbytes=50MB        ; 这个是上面那个日志文件的最大的大小,当超过50M的时候,会生成一个新的日
                               志文件。当设置为0时,表示不限制文件大小
                               默认值是50M,非必须设置。             
logfile_backups=10           ; 日志文件保持的数量,上面的日志文件大于50M时,就会生成一个新文件。文件
                               数量大于10时,最初的老文件被新文件覆盖,文件数量将保持为10
                               当设置为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将不会启动。
                               系统的文件描述符在这里设置cat /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的时候,而且想调用XML_RPC统一管理,就需要为每个
                               supervisor设置不同的标识符了
                               默认是supervisord。。。非必需设置
;directory=/tmp              ; 这个参数是当supervisord作为守护进程运行的时候,设置这个参数的话,启动
                               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="haha",age="hehe"
                               默认为不设置。。。非必须设置
;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 ; 这个是supervisorctl本地连接supervisord的时候,本地UNIX socket
                                        路径,注意这个是和前面的[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类似,我们可以用上下键来查找前面执行过的命令
                               默认是no file的。。所以我们想要有这种功能,必须指定一个文件。。。非
                               必须设置
 
; 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/test.py -a 'hehe'
                                有一点需要注意的是,我们的command只能是那种在终端运行的进程,不能是
                                守护进程。这个想想也知道了,比如说command=service httpd start。
                                httpd这个进程被linux的service管理了,我们的supervisor再去启动这个命令
                                这已经不是严格意义的子进程了。
                                这个是个必须设置的项
;process_name=%(program_name)s ; 这个是进程名,如果我们下面的numprocs参数为1的话,就不用管这个参数
                                 了,它默认值%(program_name)s也就是上面的那个program冒号后面的名字,
                                 但是如果numprocs为多个的话,那就不能这么干了。想想也知道,不可能每个
                                 进程都用同一个进程名吧。
 
                                 
;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 。。当用设定的信号去干掉进程,退出码会被认为是expected
                                  非必须设置
;stopwaitsecs=10               ; 这个是当我们向子进程发送stopsignal信号后,到系统返回信息
                                 给supervisord,所等待的最大时间。 超过这个时间,supervisord会向该
                                 子进程发送一个强制kill的信号。
                                 默认为10秒。。非必须设置
;stopasgroup=false             ; 这个东西主要用于,supervisord管理的子进程,这个子进程本身还有
                                 子进程。那么我们如果仅仅干掉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_logfile_backups=10     ; 这个也是
;stderr_capture_maxbytes=1MB   ; 这个一样,和stdout_capture一样。 默认为0,关闭状态
;stderr_events_enabled=false   ; 这个也是一样,默认为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为多个的时候,才需要。否则默认就
                                 OK了
;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                 ; 期望或者说预料中的进程退出码,
;stopsignal=QUIT               ; 干掉进程的信号,默认为TERM,比如设置为QUIT,那么如果QUIT来干这个进程
                                 那么会被认为是正常维护,退出码也被认为是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

在这里分享几个supervisor配置样例

[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杀进程 

下面是一个使用supervisor监控的配置情况(配置中的其他默认内容在此省略)

[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

[root@op-zhongkong ~]# /etc/init.d/supervisord start

可以看到配置的各个进程在后台运行了起来。停掉某个进程后supervisor会马上自动重启该进程!!!
[root@op-zhongkong ~]# supervisorctl      
supervisor>  status
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 

停止supervisor

[root@op-zhongkong ~]# /etc/init.d/supervisord stop

可通过help了解命令的更多用法: 

[root@op-zhongkong ~]# supervisorctl      
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 

====================分享一个线上曾经用过的supervisor监控python程序的配置===================

[program:xcspam]
command=/usr/bin/python /app/web/xcspam/bin/main.py --port=93%(process_num)02d
process_name=%(program_name)s_%(process_num)02d
numprocs=16
numprocs_start=1
directory=/app/web/xcspam
user=work
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/data/log/xcspam/xcspam.log
stderr_logfile=/data/log/xcspam/xcspam_error.log
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
environment=PYTHONPATH=/app/web/xcspam, KEVIN_ENV=production
  
[program:report]
command=/usr/bin/celery -A worker worker -Q report --loglevel=INFO
directory=/app/web/xcspam/bin
user=work
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/data/log/xcspam/celery-report.log
stderr_logfile=/data/log/xcspam/celery-report_error.log
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
environment=PYTHONPATH=/app/web/xcspam, KEVIN_ENV=production
  
[program:antiwater]
command=/usr/bin/celery -A worker worker -Q antiwater --loglevel=INFO
directory=/app/web/xcspam/bin
user=work
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/data/log/xcspam/celery-antiwater.log
stderr_logfile=/data/log/xcspam/celery-antiwater_error.log
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
environment=PYTHONPATH=/app/web/xcspam, KEVIN_ENV=production
  
[program:celery-flower]
command=/usr/bin/celery flower -A worker --address=10.31.51.232 --port=9330
directory=/app/web/xcspam/bin
user=work
autostart=true
autorestart=true
stopsignal=QUIT
stdout_logfile=/data/log/xcspam/celery-flower.log
stderr_logfile=/data/log/xcspam/celery-flower_error.log
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0
environment=PYTHONPATH=/app/web/xcspam, KEVIN_ENV=production

由于默默使用supervisorctl命令查看监控的程序运行状态是交互式的,所以自己写了个简单的脚本命令superctl,可以直接查看

[root@op-zhongkong ~]# cat /usr/local/bin/superctl
/usr/bin/python /usr/local/bin/superctl -c /etc/supervisord.conf $1 $2
[root@op-zhongkong ~]# chmod 755 /usr/local/bin/superctl
 
[root@op-zhongkong ~]#  superctl status
antiwater                        RUNNING   pid 17172, uptime 0:21:23
celery-flower                    RUNNING   pid 20507, uptime 0:18:52
report                           RUNNING   pid 16752, uptime 0:21:35
xcspam:xcspam_01                 RUNNING   pid 14594, uptime 0:23:00
xcspam:xcspam_02                 RUNNING   pid 14526, uptime 0:23:01
xcspam:xcspam_03                 RUNNING   pid 14551, uptime 0:23:01
xcspam:xcspam_04                 RUNNING   pid 14641, uptime 0:22:59
xcspam:xcspam_05                 RUNNING   pid 14642, uptime 0:22:58
xcspam:xcspam_06                 RUNNING   pid 14608, uptime 0:22:59
xcspam:xcspam_07                 RUNNING   pid 14618, uptime 0:22:59
xcspam:xcspam_08                 RUNNING   pid 14674, uptime 0:22:58
xcspam:xcspam_09                 RUNNING   pid 14690, uptime 0:22:58
xcspam:xcspam_10                 RUNNING   pid 14498, uptime 0:23:02
xcspam:xcspam_11                 RUNNING   pid 14462, uptime 0:23:03
xcspam:xcspam_12                 RUNNING   pid 14463, uptime 0:23:03
xcspam:xcspam_13                 RUNNING   pid 14579, uptime 0:23:00
xcspam:xcspam_14                 RUNNING   pid 14749, uptime 0:22:58
xcspam:xcspam_15                 RUNNING   pid 14669, uptime 0:22:58
xcspam:xcspam_16                 RUNNING   pid 14512, uptime 0:23:02

====================再看下面一例====================

python程序进程一般都用supervisor进行管理
[root@localhost ~]#  cp /etc/supervisord.conf /etc/supervisord.conf.bak
[root@localhost ~]#  vim /etc/supervisord.conf            # 在文件底部添加下面两个python进程的管理配置
.........
[program:uwsgi]
command=/usr/local/python3/bin/uwsgi /usr/local/nginx/conf/uwsgi.ini
directory=/data/www/APPServer/
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

[program:main]
command=/usr/local/bin/main
directory=/usr/local/bin/
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

重启supervisor服务
[root@localhost ~]# /etc/init.d/supervisord restart
Stopping supervisord:                                      [  OK  ]
Starting supervisord:                                        [  OK  ]  

查看supervisor管理的两个进程状态
[root@localhost ~]# supervisorctl 
main           RUNNING    pid 16183, uptime 0:00:12
uwsgi          RUNNING    pid 19043, uptime 0:00:13
supervisor> status
main           RUNNING    pid 16183, uptime 0:00:17
uwsgi          RUNNING    pid 19043, uptime 0:00:18
           
查看管理的这两进程的运行情况
[root@localhost ~]# ps -ef|grep main
root     16183 16181  0 21:38 ?        00:00:00 /usr/local/bin/main
root     16207 15953  0 21:42 pts/0    00:00:00 grep main

[root@localhost ~]# ps -ef|grep uwsgi
root      16595  16064  0 21:40 pts/0    00:00:00 grep --color=auto uwsgi
root      19043  17056  0 21:44 ?          00:00:04  /usr/local/python3/bin/uwsgi /usr/local/nginx/conf/uwsgi.ini

重启supervisor之后, 也就是将其管理的所有进程都重启了
[root@localhost ~]# /etc/init.d/supervisord restart

也可以根据配置文件, 指定某一个进程进行重启
[root@localhost ~]# supervisorctl -c /etc/supervisord.conf restart main
main: stopped
main: started
[root@localhost ~]# supervisorctl -c /etc/supervisord.conf restart uwsgi
uwsgi: stopped
uwsgi: started

或者是登录到supervisorctl里面进行重启,关闭操作等
[root@localhost ~]# supervisorctl 
main           RUNNING    pid 16286, uptime 0:03:58
uwsgi          FATAL      can't find command '/usr/local/python3/bin/uwsgi'
supervisor> help

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

supervisor> restart all
supervisor> restart main
supervisor> stop uwsgi
supervisor> start uwsgi

====================常用的几个进程管理配置====================

1) 用supervisor监控nginx注意事项
因为supervisor 监控的进程必须以非daemon 方式运行. 但是nginx默认不能以deamon的方式运行!!!
所以Nginx要用supervisor 管理需要的话, 就必须在nginx.conf配置文件增加一行(添加的位置在events上面,属于main)
daemon off;    

在nginx.conf文件中添加deamon off,表示关闭守护进程方式,这样nginx在终端输入启动命令会一直执行状态,不会结束!
这样就需要通过supervisor去启动和管理nginx了.
[root@kevin ~]# vim /usr/local/nginx/conf/nginx.conf
user  www;
worker_processes  4;

daemon off;    

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
     
#pid        logs/nginx.pid;
     

events {
    worker_connections  65535;
}

重启nginx服务!

然后在supervisor中加入就行。
[root@kevin ~]# vim /etc/supervisord.conf
............
[program:nginx]
command=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
user=www

2) supervisor监控某个进程的描述信息
[root@kevin ~]# vim /etc/supervisord.conf
............
[program:meta.txn.recover.on.error]
command=/cas/bin/meta.txn.recover.on.error ; 被监控的进程路径
numprocs=1                                 ; 启动几个进程
directory=/cas/bin                         ; 执行前要不要先cd到目录去,一般不用
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启。。当然要选上了
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout

3) 监控memcache的配置描述:
[root@kevin ~]# vim /etc/supervisord.conf
............
[program:memcached]
command=memcached -u root -m 1024 -l 0.0.0.0 -p 11211
process_name=%(program_name)s
numprocs=1                    ; 启动几个进程
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启。。当然要选上了
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout

4) 用supervisor管理tomcat
使用 supervisord 监控管理的进程必须以 nodaemon 启动,而 tomcat 的 startup.sh 脚本是daemon方式的,如果不做修改的话,supervisord 会一直报错。

两种解决办法:
第一种方法: 在 tomcat的启动脚本startup.sh 中, 将
exec "$PRGDIR"/"$EXECUTABLE" start "$@" 
改为    
exec "$PRGDIR"/"$EXECUTABLE" run "$@"

[root@kevin ~]# vim /etc/supervisord.conf
............
[program:tomcat]
command=/opt/tomcat/bin/startup.sh
environment=JAVA_HOME="/usr/local/jdk/",JAVA_BIN="/usr/local/jdk/bin"
directory=/opt/tomcat
autostart = true
autorestart=true
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=10MB
startsecs=5
priority=3
stopasgroup=true
killasgroup=true

第二种方法:  不修改tomcat的启动脚本start.sh
[root@kevin ~]# vim /etc/supervisord.conf
............
[program:tomcat]
command=/opt/tomcat/bin/catalina.sh run
environment=JAVA_HOME="/usr/local/jdk/",JAVA_BIN="/usr/local/jdk/bin"
directory=/opt/tomcat
autostart = true
autorestart=true
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=10MB
startsecs=5
priority=3
stopasgroup=true
killasgroup=true

5) 用supervisor管理mysql
[root@kevin ~]# vim /etc/supervisord.conf
............
[supervisord]
nodaemon=true

[inet_http_server]
port=0.0.0.0:9001
username=admin
password=123

[program:mysql]
command=/usr/bin/pidproxy /opt/mysql/data/mysqld.pid  /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf   --basedir=/usr/local/mysql --datadir=/opt/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=root --log-error=/opt/mysql/data/mysqld.err --pid-file=/opt/mysql/data/mysqld.pid --socket=/tmp/mysql.sock --port=3306 2>&1 > /dev/null &
redirect_stderr = true

6) supervisor 监控redis & mongodb
[root@kevin ~]# vim /etc/supervisord.conf
............
[supervisord]
nodaemon=true

[program:redis]
command=/usr/local/bin/redis-server /usr/local/etc/redis.conf
autostart=true
autorestart=true
startsecs=3

[program:mongod]
command=/usr/local/bin/mongod --dbpath /Users/weixuan/Database/mongodbdata
autostart=true
startsecs=3
autorestart=true

如果启动supervisor服务时报错如下
gave up: redis entered FATAL state, too many start retries too quickly
则解决办法为: 修改redis.conf的daemonize为no

======== 使用supervisor管理jenkins服务进程的配置方法 ========

[root@kevin supervisor]# pwd
/etc/supervisor

[root@kevin supervisor]# ll
total 20
drwxr-xr-x 2 root root 4096 Jul 11 18:24 conf
-rw-r--r-- 1 root root 9215 Jan 11  2019 supervisord.conf
-rw-r--r-- 1 root root    0 Apr 23  2019 supervisord.lock
-rw-r--r-- 1 root root    5 Apr 23  2019 supervisord.pid
srwx------ 1 root root    0 Apr 23  2019 supervisor.sock

[root@kevin supervisor]# cd conf
[root@kevin conf]# cat jenkins_tomcat.ini 
[program:jenkins_tomcat]
command=/root/tomcat_jenkins/bin/catalina.sh run             
process_name=%(program_name)s
numprocs=1                    
directory=/root/tomcat_jenkins/bin               
autostart=true                
startsecs=120                   
startretries=50
autorestart=unexpected 
exitcodes=0,2                 
stopsignal=QUIT               
stopwaitsecs=20
stopasgroup=false             
killasgroup=false             
user=root                 
redirect_stderr=false          
stdout_logfile=/var/log/supervisor/jenkins_tomcat_out.log    
stdout_logfile_maxbytes=1MB   
stdout_logfile_backups=10     
stdout_capture_maxbytes=1MB   
stdout_events_enabled=false   
stderr_logfile=/var/log/supervisor/jenkins_tomcat_err.log
stderr_logfile_maxbytes=1MB   
stderr_logfile_backups=10     
stderr_capture_maxbytes=1MB   
stderr_events_enabled=false   
environment=A="1",B="2"       
serverurl=AUTO  

如上配置后,jenkins服务的启停就是用supervisor来管理了
[root@kevin conf]# supervisorctl status
jenkins_tomcat                   RUNNING   pid 62388, uptime 22:27:32

jenkins服务启动、重启和关停
# supervisorctl stop jenkins_tomcat
# supervisorctl start jenkins_tomcat
# supervisorctl restart jenkins_tomcat

 

posted @ 2017-09-15 10:52  散尽浮华  阅读(25510)  评论(0编辑  收藏  举报