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:      ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性,非必须设置
;username=user ; default is no username (open server)
;password= ; 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= ;日志文件保持的数量,上面的日志文件大于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= ;这个是最少系统空闲的文件描述符,低于这个值supervisor将不会启动。系统的文件描述符在这里设置
cat /proc/sys/fs/file-max默认情况下为1024。。。非必须设置
minprocs= ;最小可用的进程描述符,低于这个值supervisor也将不会正常启动。ulimit  -u这个命令,可以查看
linux下面用户的最大进程数默认为200。。。非必须设置
;umask= ; 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= ; 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.-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= ; number of processes copies to start (def ) ;events=EVENT ; event notif. types to subscribe to (req'd) ;buffer_size= ; event buffer queue size (default ) ;directory=/tmp ; directory to cwd to before exec (def no cwd) ;umask= ; umask for process (default None) ;priority=- ; the relative start priority (default -) ;autostart=true ; start at supervisord start (default: true) ;startsecs= ; # of secs prog must stay up to be running (def. ) ;startretries= ; max # of serial start failures when starting (default ) ;autorestart=unexpected ; autorestart if exited after running (def: unexpected) ;exitcodes=, ; 'expected' exit codes used with autorestart (default ,) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs= ; max num secs to wait b4 SIGKILL (default ) ;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= ; # of stdout logfile backups ( means none, default ) ;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= ; # of stderr logfile backups ( means none, default ) ;stderr_events_enabled=false ; emit events on stderr writes (default false)

;environment=A="",B="" ; 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= ; the relative start priority (default ) ; 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 ] Connection refused: file: /usr/lib64/python2./socket.py line: 

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

[root@localhost ~]# netstat -nlp | grep
tcp6 ::: :::* LISTEN /java
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# supervisorctl status
f5_manage STARTING
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid , uptime ::
[root@localhost ~]# netstat -nlp | grep
tcp6 ::: :::* LISTEN /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模块学习使用的更多相关文章

  1. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  2. # nodejs模块学习: express 解析

    # nodejs模块学习: express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子 ...

  3. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  4. pythone函数基础(7)第三方模块学习

    一,time模块学习 import time # print(int(time.time()))#时间戳# res = time.strftime('%Y-%m-%d %H:%M:%S')#取当前格式 ...

  5. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  6. Python logging 模块学习

    logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...

  7. python - argparse 模块学习

    python - argparse 模块学习 设置一个解析器 使用argparse的第一步就是创建一个解析器对象,并告诉它将会有些什么参数.那么当你的程序运行时,该解析器就可以用于处理命令行参数. 解 ...

  8. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  9. 审计系统---paramiko模块学习

    paramiko模块学习 [更多参考]http://www.cnblogs.com/wupeiqi/articles/4963027.html [paramiko的Demo实例]https://git ...

随机推荐

  1. js学习重点难点知识总结 (巩固闭包、原型、原型链)

    学习重点知识总结   1.闭包知识点巩固        闭包函数:                    1.可以实现函数外部访问函数内部的变量                     2.在Java ...

  2. 高级脚本进阶—使用case的多功能选择性脚本

    应用场景: 在应用脚本决解实际的运维问题时,单功能脚本有很多的不同应用环境,如不同的运行环境,不同的系统版本等,这时,就需要对脚本的功能进行选择,一个脚本实现多功能多版本系统的维护,以减少沟通成本,而 ...

  3. Flink集群Standalone启动脚本(源码分析)

    整个Flink集群的角色分为Jobmanager和TaskManager 以Standalone为例来看一下脚本里面是怎样启动集群的 找到源码的dist这里面包含了启动的脚本文件 standalone ...

  4. HMM学习

    参看博客: 1.https://www.cnblogs.com/skyme/p/4651331.html 2.https://blog.csdn.net/continueoo/article/deta ...

  5. Scala 系列(二)—— 基本数据类型和运算符

    一.数据类型 1.1 类型支持 Scala 拥有下表所示的数据类型,其中 Byte.Short.Int.Long 和 Char 类型统称为整数类型,整数类型加上 Float 和 Double 统称为数 ...

  6. Docker学习总结(四)--应用部署

    MySQL部署 1) 拉取 mysql 镜像 docker pull centos/mysql:5.7 2) 创建容器 docker run -di --name=mysql -p 33306:330 ...

  7. CSS 之Grid网格大致知识梳理1

    CSS所提供的关于网格Grid属性让我们可以更方便编写页面以及布局,而它的一些主要应用属性如下: 1.将父容器的display属性值设置为grid 即可将其转换为网格容器: 2.在网格容器中添加列的属 ...

  8. MySQL5.7.27报错[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated

    mysql5.7.27在运行更新语句时出现如下情况,mysql5.6之前没有这种情况出现. of ORDER BY clause is not in GROUP BY clause and conta ...

  9. Covered Points Count CF1000C 思维 前缀和 贪心

     Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  10. PAT 天梯杯 L3-008. 喊山 bfs

    L3-008. 喊山 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂 ...