supervisor模块学习使用

摘要:
hypervisorrctl和hypervisord之间的通信是通过xml_Rpc完成的。服务器还可以要求客户端在操作之前提供身份验证。可以在[supervisorctl]WebServerWebServer上执行相应的配置,该服务器主要用于管理接口上的进程。WebServer实际上使用XML_RPC,您可以向主管请求数据,也可以控制主管和子进程。安装管理程序后,默认情况下不会生成任何配置文件。无需设置minprocs=200;最小的可用进程描述符,低于该描述符,主管将无法正常启动。

supervisor组件

  1. supervisord

    supervisord是supervisor的服务端程序。

    启动supervisor程序自身,启动supervisor管理的子进程,响应来自clients的请求,重启闪退或异常退出的子进程,把子进程的stderr或stdout记录到日志文件中,生成和处理Event

  2. supervisorctl

    客户端的命令行工具,提供一个类似shell的操作接口,通过它你可以连接到不同的supervisord进程上来管理它们各自的子程序,最牛逼的一点是,supervisorctl不仅可以连接到本机上的supervisord,还可以连接到远程的supervisord,当然在本机上面是通过UNIX socket连接的,远程是通过TCP socket连接的。supervisorctl和supervisord之间的通信,是通过xml_rpc完成的。服务端也可以要求客户端提供身份验证之后才能进行操作   相应的配置在[supervisorctl]

  3. Web Server

    Web Server主要可以在界面上管理进程,Web Server其实是通过XML_RPC来实现的,可以向supervisor请求数据,也可以控制supervisor及子进程。配置在[inet_http_server]

  4. XML_RPC接口

            供远程调用,supervisorctl和Web Server需要使用

  • 安装

  supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令)、客户端(用于和守护进程通信,发送管理进程的指令)、生成初始配置文件程序。

pip install supervisor
  • 生成配置文件

安装好supervisor之后,默认是没有生成配置文件的。可以通过以下命令生成配置文件(我们通常是把配置文件放到/etc/下面,当然也可以放到任意路径下面)

echo_supervisord_conf > /etc/supervisord.conf

配置文件里每一行开头都是分号;这个符号用来表示注释,去掉需要配置项的分号并作修改即可

[unix_http_server] 

file=/home/supervisor.sock ;socket文件的路径,supervisorctl用XML_RPC和supervisord通信就是通过它进行
的。如果不设置的话,supervisorctl也就不能用了,非必须设置
;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)

[inet_http_server]          ;侦听在TCP上的socket,Web Server和远程的supervisorctl都要用到他(提供web管理界面),非必须

port=xx.xx.xx.xxx:9001 ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性,非必须设置
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)

[supervisord]                ; 主要是定义supervisord这个服务端进程的一些参数的,必须设置

logfile=/home/supervisord.log ; supervisor服务的日志文件 main log file; default $CWD/supervisord.log,$CWD为当前目录非必须设置
logfile_maxbytes=50MB ; 日志文件大小当超过50M的时候,会生成一个新的日志文件。当设置为0时,表示不限制文件大小,默认值为50M,非必须设置
logfile_backups=10 ;日志文件保持的数量,上面的日志文件大于50M时,就会生成一个新文件。文件数量大于10时,最初的老文件被新文件覆盖,
文件数量将保持为10当设置为0时,表示不限制文件的数量默认情况下为10。。。非必须设置
loglevel=warn ; 日志级别,默认是info log level; default info; others: debug,warn,trace。程序稳定的情况下,维护用warn就够了
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default 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 ; 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]    ;给XML_RPC用的,如果想使用supervisord或者web server 都必须设置

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];supervisorctl的一些配置
serverurl=unix:///home/supervisor.sock   ;use a unix:// URL  for a unix socket这个是supervisorctl本地连接supervisord的时候,本地UNIX socket
路径,注意这个是和前面的[unix_http_server]对应的默认值就是unix:///tmp/supervisor.sock。。非必须设置
serverurl=http://xx.21.21.xxx:9001 ;use an http:// url to specify an inet socket这个是supervisorctl远程连接supervisord的时候,
用到的TCP socket路径注意这个和前面的[inet_http_server]对应默认就是http://127.0.0.1:9001。非必须项
;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:f5_manage]
command=nohup java -jar /home/soft/jarpackage/sr_f5_manage-0.0.1-SNAPSHOT.jar &      ; the program (relative uses PATH, can take args)
;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                ; 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 = relative/directory/*.ini
  • 启动:
1.supervisord  #直接启动
2.supervisord -c /etc/supervisord.conf #指定配置文件启动服务
[root@localhost tmp]# supervisord
/usr/lib/python2.7/site-packages/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '
Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.
For help, use /usr/bin/supervisord -h

①【启动错误】如果报这个错是因为supervisord已经在运行了,

ps -ef | grep supervisord 
[root@localhost tmp]# ps -ef | grep supervisord 
root      2886     1  0 16:07 ?        00:00:02 /usr/bin/python /usr/bin/supervisord
#下面这行的意思是用户root在执行grep --color=auto
supervisord的命令,不用管
root 3076 2588 0 19:53 pts/0 00:00:00 grep --color=auto supervisord
#杀掉supervisord服务
kill -9 2886
#再次启动服务
root@localhost tmp]# supervisord
#下面的提示是希望启动服务的时候指定具体的配置文件而非默认的,
#当服务有几个不同的配置文件的时候,每次启动服务的时候可以根据业务需求选择使用的配置文件
/usr/lib/python2.7/site-packages/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
#这个提示,每次重启都会提示这个,没有搞明白为什么,有什么影响
Unlinking stale socket /tmp/supervisor.sock
#可以执行下面的命令解决,
sudo unlink /tmp/supervisor.sock

②【使用supervisorctl命令行报错】一开始supervisorctl没有配置好就启动服务了,更改配置后要重新启动supervisord服务才可以使用命令行

error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 571

③【直接kill掉supervisord之后又重启,子进程会出现异常】在关闭supervisord服务之前需要先手工关闭被supervisor启动的子进程。如果直接关掉supervisord服务,那么被管理的子进程变为孤儿进程,还存在,之后再重启supervisord,它会一直尝试启动子进程。supervisorctl status查到的pid一直在动态变化,其实就是在尝试启动被管理的服务。手工杀掉之前的孤儿进程才会正常

[root@localhost ~]# netstat -nlp | grep 8007
tcp6       0      0 :::8007                 :::*                    LISTEN      711/java            
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 791, uptime 0:00:04
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 861, uptime 0:00:05
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 885, uptime 0:00:02
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 885, uptime 0:00:05
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 885, uptime 0:00:08
[root@localhost ~]# supervisorctl status
f5_manage                        STARTING  
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid
935, uptime 0:00:03 [root@localhost ~]# supervisorctl status f5_manage RUNNING pid 1006, uptime 0:00:03 [root@localhost ~]# netstat -nlp | grep 8007 tcp6 0 0 :::8007 :::* LISTEN 711/java

 [root@localhost ~]# kill -9 711
 [root@localhost ~]# netstat -nlp | grep 8007
 tcp6 0 0 :::8007 :::* LISTEN 2551/java
 [root@localhost ~]# netstat -nlp | grep 8007
 tcp6 0 0 :::8007 :::* LISTEN 2551/java
 [root@localhost ~]# netstat -nlp | grep 8007
 tcp6 0 0 :::8007 :::* LISTEN 2551/java
 [root@localhost ~]# supervisorctl status f5_manage
 f5_manage RUNNING pid 2551, uptime 0:00:30

ps:正确的操作

①通过supervisorctl加载配置,重启

# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
supervisorctl update

②通过supervisorctl先停掉所有的子进程再kill  supervisord以及重启 

# 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件
supervisorctl stop all

supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启。运行supervisorctl命令,不加参数,会进入supervisor客户端的交互终端,并会列出当前所管理的所有进程

  • supervisorctl常用命令
# 停止某一个进程,program_name 为 [program:x] 里的 x
supervisorctl stop program_name
# 启动某个进程
supervisorctl start program_name
# 重启某个进程
supervisorctl restart program_name
# 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)
supervisorctl stop groupworker:
# 结束 groupworker:name1 这个进程 (start,restart 同理)
supervisorctl stop groupworker:name1
# 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件
supervisorctl stop all
# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
supervisorctl update

参考文章:

                  https://www.cnblogs.com/zhaoding/p/6257363.html

                  https://www.cnblogs.com/wswang/p/5795766.html

                  http://blog.51cto.com/lixcto/1539136

                  https://blog.csdn.net/xyang81/article/details/51555473

                  https://www.cnblogs.com/zhoujinyi/p/6073705.html

官方文档:

                  http://supervisord.org/

免责声明:文章转载自《supervisor模块学习使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇docker命令之link自动生成Makefile的全过程详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

GDB调试教程

简介   GDB(GNU debugger)是GNU开源组织发布的一个强大的UNIX下的程序调试工具。可以使用它通过命令行的方式调试程序。它使你能在程序运行时观察程序的内部结构和内存的使用情况。你也可以使用它分析程序崩溃前的发生了什么,从而找出程序崩溃的原因。相对于windows下的图形界面的VC等调试工具,它提供了更强大的功能。如果想在Windows下使...

UNIX时间戳及日期的转换与计算

UNIX时间戳是保存日期和时间的一种紧凑简洁的方法,是大多数UNIX系统中保存当前日期和时间的一种方法,也是在大多数计算机语言中表示日期和时间的一种标准格式。以32位整数表示格林威治标准时间,例如,使用证书11230499325表示当前时间的时间戳。UNIX时间戳是从1970年1月1日零点(UTC/GMT的午夜)开始起到当前时间所经过的秒数。1970年1月...

GDB十几分钟教程

GDB十几分钟教程 笔者: liigo原文链接: http://blog.csdn.net/liigo/archive/2006/01/17/582231.aspx日期: 2006年1月16日 本文写给主要工作在Windows操作系统下而又须要开发一些跨平台软件的程序猿朋友,以及程序爱好者。 GDB是一个由GNU开源组织公布的、UNIX/LINUX操作...

UNIX网络编程——send与recv函数详解

#include <sys/socket.h> ssize_t recv(int sockfd, void *buff, size_t nbytes, int flags); ssize_t send(int sockfd, const void *buff, size_t nbytes, int flags); send和recv的前3个...

Linux/Unix下的任务管理器top命令

Windows下的任务管理器虽然不好用(个人更喜欢Process Explorer些),但也算方便,可以方便的查看进程,CPU,内存...也可以很容易的结束进程 没有图形化界面下的Linux,也有命令可以实现Windows的任务管理器功能,这个命令就是"top",用户可以使用top来对进程排序,结束进程等. top 命令是 Linux 下常用的性能分析工...

linux rhel unix centos FreeBSD 常用命令

一:使用CentOS常用命令查看cpu more /proc/cpuinfo | grep "model name" grep "model name" /proc/cpuinfo [root@localhost /]# grep "CPU" /proc/cpuinfo model name : Intel(R) Pentium...