zoukankan      html  css  js  c++  java
  • supervisor 使用实践

    注意:Supervisor要求Python 2.4或更高版本,但不支持Python 3的任何版本。

    1. supervisor 介绍

    Supervisor是一个客户端/服务器系统,允许用户在类UNIX操作系统上控制大量进程。

    Supervisor组件:

      Supervisord: 服务器部分为Supervisord,负责启动子程序,相应客户端的命令,记录stdout,stderr。

              配置文件位于/etc/supervisord.conf中,ini风格的配置文件。

      Supervisorctl: 客户端部分为Supervisorctl,它为Supervisord服务提供一个类似shell的界面,从Supervisorctl,用户可以连接到不同的Supervisord进程,获得控制子进程状态,对子进程进行命令控制。

      Web Server: 一个功能与Supervisorctl相媲美的用户界面,可以通过浏览器进行访问。激活配置文件的[inet_http_server]部分后,可以通过访问web界面查看和控制进程状态。

    平台要求:大多数Unix系统上,python2.4以上版本。(注:任何windows,pyhton3环境都不能使用)

    2.安装supervisor并使用

      我是通过使用pip进行安装的,直接执行 pip install supervisor进行安装。也可以下载supervisor包进行安装,解压后调用 python setup.py install 进行安装也是可以的,安装简单。

      Supervisord安装完成后,运行echo_supervisord_conf,这将会在你的终端打印一个supervisor配置文件的样本。

    需要把配置文件追加到一个文件,然后利用supervisord命令的-c 选项来指定改文件问配置文件,进行supervisord服务器的启动(可根据需要对配置文件进行修改)。

    启动服务:  使用supervisord 可以直接以守护进程的方式启动进程。

      一般常用的参数(其他参数可以使用-h,--help进行查看):

          -c FILE, --configuration=FILE: 指定supervisord的配置文件。

          -n, --nodaemon: 在前台启动supervisord。

          -u USER, --user=USER: 设置启动supervisord的用户。

          -d PATH, --directory=PATH: 设置supervisord的工作目录。

          -l FILE, --logfile=FILE:  设置supervisord的日志文件路径。

          -y BYTES, --logfile_maxbytes=BYTES: 日志文件最大大小,到达大小后会进行日志分割。

          -y NUM, --logfile_backups=NUM:  保留的supervisord日志的副本数。

          -j FILE, --pidfile=FILE: pid文件的路径。

    启动supervisord示例

      supervisord -c supervisord.conf

      supervisord -n 

    使用supervisorctl:

      第一种: 执行supervisorctl命令,将显示一个shell,使用该控制台控制当前由supervisord管理的进程,输入help 获取相关支持的命令。

      第二种: 执行“一次性”命令,例如: supervisorctl stop all / supervisorctl status all  (会返回执行状态)

    supervctl命令行选项:

          -c, --configuration: 配置文件路径(默认是/etc/supervisord.conf)

          -i, --interactive: 执行命令后启动交互式shell

          -s, --serverurl URL: supervisord服务器正在监听的URL

          -u, --username: 用于与服务器验证的用户名

          -p, --password: 用于与服务器验证的密码

          -r, --history-file: 保持readline历史记录

    3. 配置文件

    Supervisor配置文件通常命名为supervisord.conf。它由supervisord和supervisorctl使用。如果任一应用程序没用使用 -c 指定配置文件情况下启动,应用程序将按照指定的顺序在一下位置查找名为supervisord.conf的文件。它将使用它找到的第一个文件。

      1. $CWD/supervisord.conf

      2. $CWD/etc/supervisord.conf

      3. /etc/supervisord.conf

      4. /etc/supervisor/supervisord.conf(supervisor 3.3.0以上版本)

      5. ../etc/supervisord.conf(相对于可执行文件)

      6. ../supervisord.conf(相对于可执行文件)

    文件格式:

      supervisord.conf是一个Windows-INI风格的(python Configparser)文件,它包含段(内个段都用[header])和段内的键值对。

    环境变量:

      使用python字符串表达式语法 %(ENV_X)s ,可以在配置文件中使用在supervisord启动时环境中存在的环境变量: 

    [program: example]
    command = /usr/bin/example --loglevel=%(ENV_LOGLEVEL)s
    

    在上栗中,表达式 %(ENV_LOGLEVEL)将被扩展为环境变量 LOGLEVEL 的值。

    注意:  在supervisor3.2 及更高版本中,所有选项都支持%(ENV_X)s表达式。

    配置文件大同小异,具体的配置最好是看官网来的快, 这里给一个我的样例,如下。

    配置样例:  

    [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=helei              ; default is no username (open server)
    password=123456             ; default is no password (open server)
    
    [inet_http_server]         ; inet (TCP) server disabled by default
    port=:9001        ; ip_address:port specifier, *:port for all iface
    username=helei              ; default is no username (open server)
    password={SHA}21950a827d7e7b4e023795ffd31d5a2f47369148             ; 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=blather               ; 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 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
    
    [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=helei              ; should be same as in [*_http_server] if set
    password=123456                ; should be same as in [*_http_server] if set
    prompt=mysupervisor         ; cmd line prompt (default "supervisor")
    history_file=~/.supervisorcrl_history  ; use readline history if available
    
    
    [program:filebeat]
    command=/elk/filebeat-6.2.4-linux-x86_64/filebeat -c filebeat.yml --strict.perms=false            ; 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)
    numprocs_start=7
    directory=/elk/filebeat-6.2.4-linux-x86_64                ; 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,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=root                  ; 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)
    ;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)
    ;environment=A="1",B="2"       ; process environment additions (def no adds)
    ;serverurl=AUTO                ; override serverurl computation (childutils)
    
    ;[group:thegroupname]
    ;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
    ;priority=999                  ; the relative start priority (default 999)
    
    [include]
    files = relative/directory/*.ini
    [program:foo]
    command = /bin/cat
    包含两个项目foo和filebeat

    具体的设置选项可以看官网:http://www.supervisord.org/configuration.html#unix-http-server-section-settings

    3. 子进程

    fasdfasd

  • 相关阅读:
    开源项目:张帅个人博客
    django url映射的时候指定默认参数
    django 自定义url转换器
    django跳转页面传参
    django中're_path'的用法
    url详解
    django 用户与权限管理
    Python项目搬迁,快捷导出环境依赖包到requirements.txt
    centos安装python3.7
    更换国内pip
  • 原文地址:https://www.cnblogs.com/40kuai/p/9134998.html
Copyright © 2011-2022 走看看