美文网首页
使用supervisor工具对服务进程做保活管理

使用supervisor工具对服务进程做保活管理

作者: 就想叫yoko | 来源:发表于2019-12-07 13:23 被阅读0次

    使用supervisor工具对我们的非daemon程序做保活,当我们的进程崩溃了或被误杀了后能再次被自动拉起。

    CentOS 安装 supervisor

    $yum install supervisor -y
    

    安装完成后,使用如下命令生成默认配置文件/etc/supervisord.conf

    $echo_supervisord_conf > /etc/supervisord.conf
    

    该默认配置文件相当于是supervisor的示例配置,大部分配置都以注释的形式包含在里面了(supervisor配置文件中以分号;开始的为注释),我们可以把它当成一个小型文档来查阅。

    编辑配置文件,在尾部加入如下内容,表示supervisor将读取 /etc/supervisord.d/ 下的ini文件:

    [include]
    files = /etc/supervisord.d/*.ini
    

    我们的demo应用

    使用shell脚本编写一个测试用的demo应用,功能是每间隔1秒向/tmp/demo.log中写入一行日志,日志内容为当前时间加上demo自身的进程号,30秒后正常退出。代码如下:

    #!/bin/bash
    
    for ((i=1; i<=30; i ++))
    do
        echo `date`, $$ >> /tmp/demo.log
        sleep 1
    done
    

    向supervisor添加我们的demo应用

    创建我们demo应用的配置

    $touch /etc/supervisord.d/demo.ini
    

    写入如下内容(假设我们刚才的demo脚本存放在/tmp/路径下):

    [program:demo]
    command=sh /tmp/demo.sh
    

    启动supervisor

    $service supervisord start
    
    # 检查supervisord进程是否运行
    $ps -ef | grep supervisord
    # 输出如下信息,表示supervisord启动成功
    # root     17659     1  0 10:15 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
    
    # 检查demo进程是否运行
    $ps -ef | grep demo.sh
    

    经测试:

    1. demo进程在运行30秒后正常退出,demo不会被重新启动
    2. 如果在30秒内使用kill -9杀掉demo进程,demo将被重新拉起

    进程退出后是否重新启动与autorestartexitcodes两个配置项相关:

    1. 如果autorestart为true,则不管进程退出时exit code(程序退出码)是多少,都会重新启动
    2. 如果autorestart为false,则不管进程退出时exit code是多少,都不会重新启动
    3. 如果autorestart为unexpected,则进程退出后,如果exit code为exitcodes配置的值,则不重新启动,反之则重新启动

    autorestart的默认值为unexpected;exitcodes的默认值为0

    如果我们想让被管理的进程在任何情况下退出后都被重新启动(比如我们的demo在运行30秒后会正常退出,我们希望它退出后继续启动,一直循环),则修改/etc/supervisord.d/demo.ini成如下:

    [program:demo]
    command=sh /tmp/demo.sh
    autorestart=true
    

    停止supervisor

    $service supervisord stop
    

    注意,停止supervisord时,我们的demo进程也会随之结束运行。

    使用supervisor操作它所管理的任务

    # 查看所有任务的状态
    $supervisorctl status all
    # 输出信息如下:
    # demo                             RUNNING   pid 19810, uptime 0:00:13
    
    # 停止一个任务
    $supervisorctl stop demo
    
    # 开启一个任务
    $supervisorctl start demo
    
    # 重启一个任务
    $supervisorctl restart demo
    

    其它

    最后贴上默认配置文件

    ; 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".
    ;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
    
    [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))
    
    ;[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           ; (num of main logfile rotation backups;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)
    minfds=1024                  ; (min. avail startup file descriptors;default 1024)
    minprocs=200                 ; (min. avail process descriptors;default 200)
    ;umask=022                   ; (process file creation umask;default 022)
    ;user=chrism                 ; (default is current user, required if root)
    ;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
    ;directory=/tmp              ; (default is not to cd during start)
    ;nocleanup=true              ; (don't clean up tempfiles at start;default false)
    ;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
    ;environment=KEY="value"     ; (key value pairs to add to environment)
    ;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)
    
    ; the 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]
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
    
    [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 http_username if set
    ;password=123                ; should be same as http_password if set
    ;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
    ;history_file=~/.sc_history  ; use readline history if available
    
    ; 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]
    ;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)
    ;autorestart=unexpected        ; whether/when to restart (default: unexpected)
    ;startsecs=1                   ; number of secs prog must stay running (def. 1)
    ;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 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 (default 10)
    ;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
    ;stdout_events_enabled=false   ; emit events on stdout writes (default false)
    ;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
    ;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
    ;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
    ;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
    ;stderr_events_enabled=false   ; emit events on stderr writes (default false)
    ;environment=A="1",B="2"       ; process environment additions (def no adds)
    ;serverurl=AUTO                ; override serverurl computation (childutils)
    
    ; The 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]
    ;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)
    ;autorestart=unexpected        ; whether/when to restart (default: unexpected)
    ;startsecs=1                   ; number of secs prog must stay running (def. 1)
    ;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 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 (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        ; # of stderr logfile backups (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 below sample group section shows all possible group values,
    ; create one or more 'real' group: sections to create "heterogeneous"
    ; process groups.
    
    ;[group:thegroupname]
    ;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
    ;priority=999                  ; the relative start priority (default 999)
    
    ; The [include] section can just contain the "files" setting.  This
    ; setting can list multiple files (separated by whitespace or
    ; newlines).  It can also contain wildcards.  The filenames are
    ; interpreted as relative to this file.  Included files *cannot*
    ; include files themselves.
    
    [include]
    files = /etc/supervisord.d/*.ini
    

    原文链接: https://pengrl.com/p/64791/
    原文出处: yoko blog (https://pengrl.com)
    原文作者: yoko (https://github.com/q191201771)
    版权声明: 本文欢迎任何形式转载,转载时完整保留本声明信息(包含原文链接、原文出处、原文作者、版权声明)即可。本文后续所有修改都会第一时间在原始地址更新。

    本篇文章由一文多发平台ArtiPub自动发布

    相关文章

      网友评论

          本文标题:使用supervisor工具对服务进程做保活管理

          本文链接:https://www.haomeiwen.com/subject/uzkugctx.html