• Supervisor


    一、简介

    	Supervisor是一个进程管理工具,当进程中断的时候Supervisor能自动重新启动它。可以运行在各种类unix的机器上,supervisor就是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
    

    1.1 supervisord

    	运行 Supervisor 时会启动一个进程 supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启。
    	supervisor是所有进程的父进程,管理着启动的子进展,supervisor以子进程的PID来管理子进程,当子进程异常退出时supervisor可以收到相应的信号量。
    

    1.2 supervisorctl

    	是命令行管理工具,可以用来执行 stop、start、restart 等命令,来对这些子进程进行管理。
    	
    	supervisorctl是supervisor的命令行客户端(command-line client),提供类似shell的命令 交互功能。supervisorctl通过使用UNIX域套接字或Internet(TCP)套接字与supervisord服务端进行通信
    

    1.3 平台要求

    1. 各种类unix的机器上可以正常运行,比如Linux(Ubuntu 9.10),Mac OS X(10.4 / 10.5 / 10.6)和Solaris(10 for Intel)和FreeBSD 6.1
    2. 在任何版本的Windows下,Supervisor 都不会运行。
    3. Supervisor 可以使用Python 2.4或更高版本,但不能在任何版本的Python 3下使用

    二、安装

    2.1 yum or apt

    $ yum install -y supervisor
    $ apt-get install supervisor
    

    2.2 pip (推荐)

    	一般 linux 服务器 会自带 2.4版本以上的python,可以直接调用 pip,所以比较推荐这种安装方式
    
    $ pip install supervisor
    

    2.3 下载包安装

    wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz
    tar zxvf supervisor-3.1.3.tar.gz
    cd supervisor-3.1.3
    python setup.py install
    

    安装完成后,查看是否安装成功

    echo_supervisord_conf
    

    三、配置

    3.1 主配置文件

    1. 如果使用 yum 或者 apt 安装,会生成生成 supervisor的主配置文件,路径在/etc/supervisor/supervisord.conf 或者/etc/supervisord.conf 。同时还会生成配置目录 /etc/supervisord.d
    2. 如果使用其他安装方式,需要手动添加。
    $ mkdir /etc/supervisor
    $ echo_supervisord_conf > /etc/supervisor/supervisord.conf
    

    3.2 子进程配置文件

    	supervisor的配置文件默认是不全的,子进程的配置文件需要另外指定(改主配置的话所有管理的子进程会一起改掉)
    
    $ mkdir /etc/supervisor/supervisord.d/
    
    	在`/etc/supervisor/supervisord.d`的目录下创建`conf`和`log`两个目录,`conf`用于存放管理进程的配置,`log`用于存放管理进程的日志。
    
    $ cd /etc/supervisor/supervisord.d
    $ mkdir conf log
    

    3.3 修改主配置文件

    $ vim /etc/supervisor/supervisord.conf
    

    我们先修改supervisord.conf最后的[include]部分配置:

    特别注意:原来[include] 和 files 前都有冒号,要去掉

    [include]
    files = /etc/supervisor/supervisord.d/conf/*.conf # 就是之前创建的子进程的配置目录
    

    也可以修改supervisor应用日志的目录,默认日志路径为/var/log/supervisor/supervisord.log。(一般不改,直接改子进程配置项)

    vi /etc/supervisord.conf
    ...
    [supervisord]
    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               ; (log level;default info; others: debug,warn,trace)
    pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
    ...
    

    四、启动 supervisor

    4.1 使用 pip 或者安装包安装

    $ supervisord
    

    supervisor 默认在以下路径查找配置文件:/usr/etc/supervisord.conf, /usr/supervisord.conf, supervisord.conf, etc/supervisord.conf, /etc/supervisord.conf, /etc/supervisor/supervisord.conf

    如需指定主配置文件,则需要使用-c参数:

    $ supervisord -c /etc/supervisor/supervisord.conf
    

    启动时候碰到 Unlinking stale socket /tmp/supervisor.sock,解决办法如下:

    sudo unlink /tmp/supervisor.sock 
    

    查看安装的版本:

    $ supervisord -v
    3.3.4
    

    然后查看supervisor的状态:

    $ supervisorctl status
    
    >>>
    这时候不报错就行,因为还没有守护进程
    

    注: supervisord是主进程,supervisorctl是给守护进程发送命令的客户端工具。

    4.2 使用 yum 或者 apt 安装

    $ /etc/init.d/supervisor start
    

    五、简单示例

    	先创建一个脚本 `/tmp/echo_time.sh`
    
    #/bin/bash
    
    while true; do
        echo `date +%Y-%m-%d,%H:%m:%s`
        sleep 2
    done
    

    ​ 在/etc/supervisor/supervisord.d/conf新增子进程配置文件 echo_time.conf

    [program:echo_time]
    command=sh /tmp/echo_time.sh
    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=root                 ; setuid to this UNIX account to run the program
    log_stdout=true
    log_stderr=true             ; if true, log program stderr (def false)
    logfile=/tmp/echo_time.log
    logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
    logfile_backups=10          ; # of logfile backups (default 10)
    stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认 50MB
    stdout_logfile_backups=20     ; stdout 日志文件备份数
    stdout_logfile=/etc/supervisor/supervisord.d/log/echo_time.stdout.log
    

    解释

    [group:tornadoes]
    programs=tornado-16018,tornado-16019
    
    [program:tornado-16018]
    directory=/home/XXX/PycharmProjects/NLPXXX_build/main ; 程序的启动目录
    command=python runServer.py --port=16018 ; 启动命令,可以看出与手动在命令行启动的命令是一样的
    autostart = true ; 在 supervisord 启动的时候也自动启动
    startsecs = 30 ; 启动 10 秒后没有异常退出,就当作已经正常启动了
    autorestart = true ; 程序异常退出后自动重启
    startretries = 3 ; 启动失败自动重试次数,默认是 3
    user = XXX ; 用哪个用户启动
    redirect_stderr=true ; 把 stderr 重定向到 stdout,默认 false
    stdout_logfile_maxbytes=50MB ; stdout 日志文件大小,默认 50MB
    stdout_logfile_backups=10 ; stdout 日志文件备份数
    ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
    stdout_logfile=/home/XXX/PycharmProjects/log/tornado_16018.log
    
    [program:tornado-16019]
    directory=/home/XXX/PycharmProjects/NLPXXX/main ; 程序的启动目录
    command=python runServer.py --port=16018 ; 启动命令,可以看出与手动在命令行启动的命令是一样的
    autostart = true ; 在 supervisord 启动的时候也自动启动
    startsecs = 30 ; 启动 10 秒后没有异常退出,就当作已经正常启动了
    autorestart = true ; 程序异常退出后自动重启
    startretries = 3 ; 启动失败自动重试次数,默认是 3
    user = XXX ; 用哪个用户启动
    redirect_stderr=true ; 把 stderr 重定向到 stdout,默认 false
    stdout_logfile_maxbytes=50MB ; stdout 日志文件大小,默认 50MB
    stdout_logfile_backups=10 ; stdout 日志文件备份数
    ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
    stdout_logfile=/home/damao/PycharmProjects/log/tornado_16019.log
    

    然后启动程序:

    $ supervisorctl reread
    $ supervisorctl update
    

    ​ 这两个命令分别代表重新读取配置、更新子进程组。

    ​ 执行 supervisorctl reread 后输出

    $ echo_time: available
    

    ​ 执行 supervisorctl update 后输出:

    $ echo_time: added process group
    

    ​ 这样刚才添加的echo_time脚本就常驻运行起来了。通过日志查看运行情况:

    $ tail -100f /etc/supervisor/supervisord.d/log/echo_time.stdout.log
    
    >>>
    2021-03-09,20:03:1615291846
    2021-03-09,20:03:1615291848
    2021-03-09,20:03:1615291850
    2021-03-09,20:03:1615291852
    2021-03-09,20:03:1615291854
    2021-03-09,20:03:1615291856
    2021-03-09,20:03:1615291858
    2021-03-09,20:03:1615291860
    2021-03-09,20:03:1615291862
    2021-03-09,20:03:1615291864
    2021-03-09,20:03:1615291866
    ...
    

    也可以使用supervisorctl status查看子进程运行情况:

    $ supervisorctl  status
    >>>
    echo_time      RUNNING   pid 28206, uptime 0:00:11
    

    六、配置文件

    6.1 主配置

    	主配置文件名: `supervisord.conf`。可以通过运行 `echo_supervisord_conf` 获得配置。根据`章节3.2`,已经创建在 `/etc/supervisor/supervisord.conf`,这个配置文件一般情况下不需要更改,除了最后的`[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 ".
    ;
    ; Warning:
    ;  Paths throughout this example file use /tmp because it is available on most
    ;  systems.  You will likely need to change these to locations more appropriate
    ;  for your system.  Some systems periodically delete older files in /tmp.
    ;  Notably, if the socket file defined in the [unix_http_server] section below
    ;  is deleted, supervisorctl will be unable to connect to supervisord.
    
    [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)
    
    ; Security Warning:
    ;  The inet HTTP server is not enabled by default.  The inet HTTP server is
    ;  enabled by uncommenting the [inet_http_server] section below.  The inet
    ;  HTTP server is intended for use within a trusted environment only.  It
    ;  should only be bound to localhost or only accessible from within an
    ;  isolated, trusted network.  The inet HTTP server does not support any
    ;  form of encryption.  The inet HTTP server does not use authentication
    ;  by default (see the username= and password= options to add authentication).
    ;  Never expose the inet HTTP server to the public internet.
    
    ;[inet_http_server]         ; inet (TCP) server disabled by default
    ;port=127.0.0.1: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=/tmp/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=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
    nodaemon=false               ; start in foreground if true; default false
    silent=false                 ; no logs to stdout 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=supervisord            ; setuid to this UNIX account at startup; recommended 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:///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 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                   ; 'expected' exit codes used with autorestart (default 0)
    ;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)
    ;stdout_syslog=false           ; send stdout to syslog with process name (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)
    ;stderr_syslog=false           ; send stderr to syslog with process name (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                   ; 'expected' exit codes used with autorestart (default 0)
    ;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)
    ;stdout_syslog=false           ; send stdout to syslog with process name (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)
    ;stderr_syslog=false           ; send stderr to syslog with process name (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
    

    6.2 子进程配置文件

    根据`章节3.2`设置,一般放在:`/etc/supervisor/supervisord.d/conf`目录 。一个脚本对应一个配置文件。
    

    配置说明:

    ;*为必须填写项
    ;*[program:应用名称]
    [program:cat]
    
    ;*命令路径,如果使用python启动的程序应该为 python /home/test.py, 
    ;不建议放入/home/user/, 对于非user用户一般情况下是不能访问
    command=/bin/cat
    
    ;当numprocs为1时,process_name=%(program_name)s;
    当numprocs>=2时,%(program_name)s_%(process_num)02d
    process_name=%(program_name)s
    
    ;进程数量
    numprocs=1
    
    ;执行目录,若有/home/supervisor_test/test1.py
    ;将directory设置成/home/supervisor_test
    ;则command只需设置成python test1.py
    ;否则command必须设置成绝对执行目录
    directory=/tmp
    
    ;掩码:--- -w- -w-, 转换后rwx r-x w-x
    umask=022
    
    ;优先级,值越高,最后启动,最先被关闭,默认值999
    priority=999
    
    ;如果是true,当supervisor启动时,程序将会自动启动
    autostart=true
    
    ;*自动重启
    autorestart=true
    
    ;启动延时执行,默认1秒
    startsecs=10
    
    ;启动尝试次数,默认3次
    startretries=3
    
    ;当退出码是0,2时,执行重启,默认值0,2
    exitcodes=0,2
    
    ;停止信号,默认TERM
    ;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐)
    ;终止:TERM(kill -TERM pid)
    ;挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同
    ;从容停止:QUIT(kill -QUIT pid)
    ;KILL, USR1, USR2其他见命令(kill -l),说明1
    stopsignal=TERM
    
    stopwaitsecs=10
    
    ;*以root用户执行
    user=root
    
    ;重定向
    redirect_stderr=false
    
    stdout_logfile=/a/path
    stdout_logfile_maxbytes=1MB
    stdout_logfile_backups=10
    stdout_capture_maxbytes=1MB
    stderr_logfile=/a/path
    stderr_logfile_maxbytes=1MB
    stderr_logfile_backups=10
    stderr_capture_maxbytes=1MB
    
    ;环境变量设置
    environment=A="1",B="2"
    
    serverurl=AUTO
    

    简化模板

    [program:echo_time]
    command=sh /tmp/echo_time.sh
    autostart=true
    autorestart=true
    startsecs=10
    startretries=3 
    exitcodes=0,2
    stopsignal=QUIT
    stopwaitsecs=10
    user=root
    log_stdout=true
    log_stderr=true
    logfile=/tmp/echo_time.log
    logfile_maxbytes=1MB
    logfile_backups=10 
    stdout_logfile_maxbytes=20MB 
    stdout_logfile_backups=20 
    stdout_logfile=/tmp/echo_time.stdout.log
    

    如果上面的模板里增加下面两句:

    process_name=%(process_num)02d
    numprocs=3
    

    执行supervisorctl update后会启动三个进程:

    $ supervisorctl status
    echo_time:00     RUNNING   pid 26762, uptime 2:06:12
    echo_time:01     RUNNING   pid 26866, uptime 2:06:02
    echo_time:02     RUNNING   pid 27975, uptime 2:05:52
    

    这对于需要多进程消费的场景非常有用。

    七、命令行程序

    7.1 supervisord

    supervisord 是主进程,通过supervisord -h可以查看帮助说明。示例:

    -c/--configuration FILENAME ;指定配置文件
    -n/--nodaemon ;运行在前台(调试用)
    -v/--version ;打印版本信息
    
    -u/--user USER ;以指定用户(或用户ID)运行
    -m/--umask UMASK ;指定子进程的umask,默认是022
    -l/--logfile FILENAME ;指定日志文件
    -e/--loglevel LEVEL ;指定日志级别
    

    7.2 supervisorctl

    supervisorctl 是客户端程序,用于向supervisord发起命令。

    通过supervisorctl -h可以查看帮助说明。我们主要关心的是其action命令:

    $ supervisorctl  help
    
    default commands (type help <topic>):
    =====================================
    add    exit      open  reload  restart   start   tail   
    avail  fg        pid   remove  shutdown  status  update 
    clear  maintail  quit  reread  signal    stop    version
    

    这些命令对于控制子进程非常重要。示例:

    注意:示例中的<name> 为[program:name]的 name

    reread ;重新加载配置文件
    update ;将配置文件里新增的子进程加入进程组,如果设置了autostart=true则会启动新新增的子进程
    status ;查看所有进程状态
    status <name> ;查看指定进程状态
    start all; 启动所有子进程
    start <name>; 启动指定子进程
    restart all; 重启所有子进程
    restart <name>; 重启指定子进程
    stop all; 停止所有子进程
    stop <name>; 停止指定子进程
    reload ;重启supervisord
    add <name>; 添加子进程到进程组
    reomve <name>; 从进程组移除子进程,需要先stop。注意:移除后,需要使用reread和update才能重新运行该进程
    

    supervisord 有进程组process group的概念:只有子进程在进程组,才能被运行。

    supervisorctl也支持交互式命令行:

    $ supervisorctl
    echo_time                        RUNNING   pid 27188, uptime 0:05:09
    supervisor> version
    3.3.4
    supervisor> 
    

    八、web界面操作

    需要开启supervisord.conf注释掉的这4行:

    [inet_http_server]         ; inet (TCP) server disabled by default
    port=127.0.0.1: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)
    

    端口默认是监听127.0.0.1:9001,这里方便测试(使所有机器都能访问),修改为:

    port=*:9001 
    

    注意:

    Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程。否则提示Exited too quickly (process log may have details)异常。例子中的Tomcat默认是以守护进程启动的,所以我们改成了catalina.sh run,以前台进程的方式运行。

    下面是配置Tomcat进程的一个例子:

    [program:tomcat]
    command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run
    stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
    autostart=true
    autorestart=true
    startsecs=5
    priority=1
    stopasgroup=true
    killasgroup=true
    

    九、cesi集群监控

    supervisor不支持跨机器的进程监控,一个supervisord只能监控本机上的程序,大大限制了supervisor的使用。

    不过由于supervisor本身支持xml-rpc,因此也有一些基于supervisor二次开发的多机器进程管理工具。比如:

    • Django-Dashvisor Web-based dashboard written in Python. Requires Django 1.3 or 1.4.
    • Nodervisor Web-based dashboard written in Node.js.
    • Supervisord-Monitor Web-based dashboard written in PHP.
    • SupervisorUI Another Web-based dashboard written in PHP.
    • cesi cesi is a web interface provides manage supervizors from same interface.

    安装 CeSi 有三个依赖:Python3Flasksqlite3
    Python 2.5 开始 sqlite3 已经在标准库内置了,所以也不需要安装 sqlite3 模块了; 另外很多 Linux 发行版已经自带 sqlite3,所以无需另外安装;
    只需要安装 flask web 框架即可;

    具体的安装直接看github README.md 即可

    GitHub - gamegos/cesi: CeSI is a web interface for managing multiple supervisors from the same place.

    $ export CESI_SETUP_PATH=~/cesi
    $ mkdir ${CESI_SETUP_PATH}
    $ cd ${CESI_SETUP_PATH}
    
    $ # Download the project to ~/cesi directory
    $ wget https://github.com/gamegos/cesi/releases/download/v2.7.1/cesi-extended.tar.gz -O cesi.tar.gz
    $ tar -xvf cesi.tar.gz
    
    $ # Create virtual environment and install requirement packages
    $ python3 -m venv venv
    $ source venv/bin/activate
    (venv) $ pip3 install -r requirements.txt
    
    $ # Run with command line
    (venv) $ python3 ${CESI_SETUP_PATH}/cesi/run.py --config-file ${CESI_SETUP_PATH}/defaults/cesi.conf.toml
    

    但是这里有个坑,官网建议的python版本为 python3.4,我用的是3.6版本,而在 requirements.txt 里面,已经把版本写死了:

    flask==1.1.2
    flask-sqlalchemy==2.4.3
    psycopg2-binary==2.8.5
    pymysql==0.9.3
    tomlkit==0.5.11
    

    在首次登录的时候,发现一直提示密码错误,在翻看了github 的 issues 后返现不止一个人碰到这个问题,解决方法为升级flask-sqlalchemy版本:

    pip3 uninstall flask-sqlalchemy
    pip3 install flask-sqlalchemy
    

    配置文件地址${CESI_SETUP_PATH}/defaults/cesi.conf.toml

    # This is the main CeSI toml configuration file. It contains CeSI web application and
    # supervisord information to connect
    
    # This is the CeSI's own configuration.
    [cesi]
    # Database Uri
    database = "sqlite:///users.db"                         # Relative path
    # Etc
    #database = "sqlite:////opt/cesi/< version >/users.db"  # Absolute path
    #database = "postgres://<user>:<password>@localhost:5432/<database_name>"
    #database = "mysql+pymysql://<user>:<password>@localhost:3306/<database_name>"
    activity_log = "activity.log"   # File path for CeSI logs
    admin_username = "admin"        # Username of admin user
    admin_password = "admin"        # Password of admin user
    
    # This is the definition section for new supervisord node.
    # [[nodes]]
    # name = "api"          # (String) Unique name for supervisord node.
    # environment = ""      # (String) The environment name provides logical grouping of supervisord nodes. It can be used as filtering option in the UI.
    # username = ""         # (String) Username of the XML-RPC interface of supervisord Set nothing if no username is configured
    # password = ""         # (String) Password of the XML-RPC interface of supervisord. Set nothing if no username is configured
    # host = "127.0.0.1"    # (String) Host of the XML-RPC interface of supervisord
    # port = "9001"         # (String) Port of the XML-RPC interface of supervisord
    
    # Default supervisord nodes
     [[nodes]]
     name = "products-server"
     environment = ""
     username = "tester"
     password = "t@987654321"
     host = "192.168.8.129"
     port = "9001"
    
    # [[nodes]]
    # name = "analysis-server"
    # environment = ""
    # username = "user123"
    # password = "pass123"
    # host = "analysis.example.com"
    # port = "9001"
    
    # [[nodes]]
    # name = "monitoring-server"
    # environment = ""
    # username = ""
    # password = ""
    # host = "monitoring.example.com"
    # port = "9001"
    

    参考:

    1、Supervisor使用教程 - 飞鸿影 - 博客园 (cnblogs.com)

  • 相关阅读:
    蟠桃记
    考新郎
    有假币
    年会抽奖
    发邮件
    进制回文数
    数位和
    外星人的语言
    一的个数
    继承
  • 原文地址:https://www.cnblogs.com/dongye95/p/14507350.html
Copyright © 2020-2023  润新知