zoukankan      html  css  js  c++  java
  • 斑马斑马-13-白云之上- supervisor 进程管理

    一、简介

      Supervisor(http://supervisord.org) 是用 Python 开发的一个 C/S 服务。是 Linux/Unix 系统下的进程管理工具。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

      supervisord 运行Supervisor时会启动一个进程supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启。

      supervisorctl 是命令行管理工具,可以用来执行 stop、start、restart 等命令,来对这些子进程进行管理。

      supervisor是所有进程的父进程,管理着启动的子进展,supervisor以子进程的PID来管理子进程,当子进程异常退出时supervisor可以收到相应的信号量。

    二、安装

    1:通过pip安装

    pip install supervisor

    2:检查是否安装成功

      安装路径:/root/.virtualenvs/jingblog/bin(我是在虚拟环境下安装的)

       执行echo_supervisord_conf

    三、Hello Supervisor

    1:创建目录,初始化配置文件

      mkdir /usr/supervisor

      echo_supervisord_conf > /usr/supervisor/supervisord.conf

      echo_supervisord_conf详解

    ; 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=0: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 = /usr/supervisor/supervisord.d/*.conf
    supervisord.conf

    2:为了不将所有新增配置信息全写在一个配置文件里,这里新建一个文件夹

     mkdir /usr/supervisor/supervisord.d/

    3:修改系统配置文件

      vim /usr/supervisor/supervisord.conf

      3.1 include 2.1中新建的supervisord.d文件夹下所有的conf    

    [include]
    files = /usr/supervisor/supervisord.d/*.conf

      3.2 若需要web查看进程,则去掉[inet_http_server]的注释,并修改响应的ip配置 

    [inet_http_server]
    port=127.0.0.1:9001   ;IP按需配置     
    username=user              
    password=123

    4:如果是阿里云服务器,还需要开放9001端口

    5:运行Supervisor

      启动supervisord

      supervisord -c /usr/supervisor/supervisord.conf  

       查看监听

      lsof -i:9001

       

       查看网页

    四、配置Supervisor

    1:添加一个配置文件,以便supervisor用来监控程序

      cat > /usr/supervisor/supervisord.d/supervisor_project_myblogs.conf

    - command:启动程序使用的命令,可以是绝对路径或者相对路径
    - process_name:一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)s
    - numprocs:Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1
    - numprocs_start:一个int偏移值,当启动实例的时候用来计算numprocs的值
    - priority:权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999
    - autostart:如果设置为true,当supervisord启动的时候,进程会自动重启。
    - autorestart:值可以是false、true、unexpected。false:进程不会自动重启,unexpected:当程序退出时的退出码不是exitcodes中定义的时,进程会重启,true:进程会无条件重启当退出的时候。
    - startsecs:程序启动后等待多长时间后才认为程序启动成功
    - startretries:supervisord尝试启动一个程序时尝试的次数。默认是3
    - exitcodes:一个预期的退出返回码,默认是0,2- stopsignal:当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2。
    - stopwaitsecs:在操作系统给supervisord发送SIGCHILD信号时等待的时间
    - stopasgroup:如果设置为true,则会使supervisor发送停止信号到整个进程组
    - killasgroup:如果设置为true,则在给程序发送SIGKILL信号的时候,会发送到整个进程组,它的子进程也会受到影响。
    - user:如果supervisord以root运行,则会使用这个设置用户启动子程序
    - redirect_stderr:如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符。
    - stdout_logfile:把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置。
    - stdout_logfile_maxbytes:标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
    - stdout_logfile_backups:标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份
    - stdout_capture_maxbytes:当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
    - stdout_events_enabled:如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发
    - stderr_logfile:把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为true
    - stderr_logfile_maxbytes:错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小
    - stderr_logfile_backups:错误日志轮转备份的数量,默认是10,如果设置为0,则不备份
    - stderr_capture_maxbytes:当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB
    - stderr_events_enabled:如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发
    - environment:一个k/v对的list列表
    - directory:supervisord在生成子进程的时候会切换到该目录
    - umask:设置进程的umask
    - serverurl:是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url
    配置文件,详解
    [program:myblogs]
    command=python3 manage.py runserver 0:8089 ; 被监控的进程启动命令
    directory=/opt/project/jblog/myblogs               ; 执行前要不要先cd到目录去,一般不用
    priority=1                    ;数字越高,优先级越高
    numprocs=1                    ; 启动几个进程
    autostart=true                ; 随着supervisord的启动而启动
    autorestart=true              ; 自动重启。。当然要选上了
    startretries=10               ; 启动失败时的最多重试次数
    exitcodes=0                   ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
    stopsignal=KILL               ; 用来杀死进程的信号
    stopwaitsecs=10               ; 发送SIGKILL前的等待时间
    redirect_stderr=true          ; 重定向stderr到stdout
    supervisor_project_myblogs.conf

    2:重启supervisor

      supervisorctl -c /usr/supervisor/supervisord.conf

      reload

      

       现在就可以在http://ip:9001/中访问test_one的程序了。

      

    3:测试

      找到8089端口对应的进程

      lsof -i:8089
      杀死

      

    五、Supervisor开机自动启动

    1:在目录/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 /usr/supervisor/supervisord.conf ;开机启动时执行
    ExecStop=/usr/bin/supervisord shutdown
    ExecReload=/usr/bin/supervisord reload
    killMode=process
    Restart=on-failure
    RestartSec=42s
    
    [Install]
    WantedBy=multi-user.target
    supervisord.service

    2:启动服务

      systemctl enable supervisord

    3:验证一下是否为开机启动

      systemctl is-enabled supervisord

    六、Supervisor周边

    1:常用命令

    [root@iZ8vbi4t4rsi4king2impwZ system]# 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
    View Code
    update 更新新的配置到supervisord(不会重启原来已运行的程序)
    reload,载入所有配置文件,并按新的配置启动、管理所有进程(会重启原来已运行的程序)
    start xxx: 启动某个进程
    restart xxx: 重启某个进程
    stop xxx: 停止某一个进程(xxx),xxx为[program:theprogramname]里配置的值
    stop groupworker: 重启所有属于名为groupworker这个分组的进程(start,restart同理)
    stop all,停止全部进程,注:start、restart、stop都不会载入最新的配置文
    reread,当一个服务由自动启动修改为手动启动时执行一下就ok
     

    2:常见问题

    2.1 如果supervisorCPU和内存占用率特别高,导致机器卡死。 

      解决方案:若需要关闭web查看进程,则注释掉[inet_http_server], 

    2.2 Unlinking stale socket /tmp/supervisor.sock

      解决方案:执行unlink /tmp/supervisor.sock

  • 相关阅读:
    罗杨美慧 20180912-3 词频统计
    罗杨美慧 20190912-2 命令行
    罗杨美慧 20190905-1 每周例行报告
    罗杨美慧 20190905-2 博客作业
    20190919-4 测试,结对要求
    孙晓宇-20190912-1 每周例行报告
    孙晓宇-20180912-3 词频统计
    (第二周)孙晓宇20190912-2 命令行
    孙晓宇20190905-2 博客作业
    孙晓宇20190905-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/YK2012/p/12867061.html
Copyright © 2011-2022 走看看