Supervisor

    xiaoxiao2021-03-25  145

    Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用 supervisor 同时启动所有应用程序而不是一个一个地敲命令启动。

     

    安装supervisor

    Debian / Ubuntu可以直接通过apt安装:

    # apt-get install supervisor

     

    supervisord 配置

    Supervisor 相当强大,提供了很丰富的功能,不过我们可能只需要用到其中一小部分。安装完成之后,可以编写配置文件,来满足自己的需求。为了方便,我们把配置分成两部分:supervisord(supervisor 是一个 C/S 模型的程序,这是 server 端,对应的有 client 端:supervisorctl)和应用程序(即我们要管理的程序)。

    首先来看 supervisord 的配置文件。安装完supervisor 之后,可以运行echo_supervisord_conf 命令输出默认的配置项,也可以重定向到一个配置文件里

    1

    echo_supervisord_conf > /etc/supervisord.conf

    去除里面大部分注释和“不相关”的部分,我们可以先看这些配置:

    [unix_http_server]file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用;chmod=0700 ; socket 文件的 mode,默认是 0700;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid;[inet_http_server] ; HTTP 服务器,提供 web 管理界面;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性;username=user ; 登录管理后台的用户名;password=123 ; 登录管理后台的密码[supervisord]logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.loglogfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MBlogfile_backups=10 ; 日志文件保留备份数量默认 10loglevel=info ; 日志级别,默认 info,其它: debug,warn,tracepidfile=/tmp/supervisord.pid ; pid 文件nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024minprocs=200 ; 可以打开的进程数的最小值,默认 200; 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 ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord; 包含其他的配置文件[include]files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    [unix_http_server]

    file=/tmp/supervisor.sock   ; UNIX socket 文件,supervisorctl 会使用

    ;chmod=0700                 ; socket 文件的 mode,默认是 0700

    ;chown=nobody:nogroup       ; socket 文件的 owner,格式: uid:gid

     

    ;[inet_http_server]         ; HTTP 服务器,提供 web 管理界面

    ;port=127.0.0.1:9001        ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性

    ;username=user              ; 登录管理后台的用户名

    ;password=123               ; 登录管理后台的密码

     

    [supervisord]

    logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log

    logfile_maxbytes=50MB        ; 日志文件大小,超出会 rotate,默认 50MB

    logfile_backups=10           ; 日志文件保留备份数量默认 10

    loglevel=info                ; 日志级别,默认 info,其它: debug,warn,trace

    pidfile=/tmp/supervisord.pid ; pid 文件

    nodaemon=false               ; 是否在前台启动,默认是 false,即以 daemon 的方式启动

    minfds=1024                  ; 可以打开的文件描述符的最小值,默认 1024

    minprocs=200                 ; 可以打开的进程数的最小值,默认 200

     

    ; 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 ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致

    ;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord

     

    ; 包含其他的配置文件

    [include]

    files = relative/directory/*.ini    ; 可以是 *.conf 或 *.ini

    我们把上面这部分配置保存到 /etc/supervisord.conf(或其他任意有权限访问的文件),然后启动 supervisord(通过 -c 选项指定配置文件路径,如果不指定会按照这个顺序查找配置文件:$CWD/supervisord.conf, $CWD/etc/supervisord.conf,/etc/supervisord.conf):

    1

    supervisord -c /etc/supervisord.conf

     

    现在编写一份配置文件来管理这个进程(需要注意:用 supervisord 管理时,gunicorn 的 daemon 选项需要设置为 False):

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    [program:usercenter]

    directory = /home/leon/projects/usercenter ; 程序的启动目录

    command = gunicorn -c gunicorn.py wsgi:app  ; 启动命令,可以看出与手动在命令行启动的命令是一样的

    autostart = true     ; 在 supervisord 启动的时候也自动启动

    startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了

    autorestart = true   ; 程序异常退出后自动重启

    startretries = 3     ; 启动失败自动重试次数,默认是 3

    user = leon          ; 用哪个用户启动

    redirect_stderr = true  ; 把 stderr 重定向到 stdout,默认 false

    stdout_logfile_maxbytes = 20MB  ; stdout 日志文件大小,默认 50MB

    stdout_logfile_backups = 20     ; stdout 日志文件备份数

    ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)

    stdout_logfile = /data/logs/usercenter_stdout.log

     

    ; 可以通过 environment 来添加需要的环境变量,一种常见的用法是修改 PYTHONPATH

    ; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere

     

    重启supervisor,让配置文件生效,然后运行命令supervisorctl启动进程:

    # supervisorctl start app

    停止进程:

    # supervisorctl stop app

     

    supervisorctl status  查看状态

    supervisorctl  help  查看命令

     

    使用 supervisorctl

    Supervisorctl 是 supervisord 的一个命令行客户端工具,启动时需要指定与 supervisord 使用同一份配置文件,否则与 supervisord 一样按照顺序查找配置文件。

    supervisorctl -c /etc/supervisord.conf

    1

    supervisorctl -c /etc/supervisord.conf

    上面这个命令会进入 supervisorctl 的 shell 界面,然后可以执行不同的命令

    1

    2

    3

    4

    5

    6

    > status    # 查看程序状态

    > stop usercenter   # 关闭 usercenter 程序

    > start usercenter  # 启动 usercenter 程序

    > restart usercenter    # 重启 usercenter 程序

    > reread    # 读取有更新(增加)的配置文件,不会启动新添加的程序

    > update    # 重启配置文件修改过的程序

    上面这些命令都有相应的输出,除了进入 supervisorctl 的 shell 界面,也可以直接在 bash 终端运行:

    1

    2

    3

    4

    5

    6

    $ supervisorctl status

    $ supervisorctl stop usercenter

    $ supervisorctl start usercenter

    $ supervisorctl restart usercenter

    $ supervisorctl reread

    $ supervisorctl update

     

     

    例:supervisortornado 多进程多端口配置

    base: nginx tornado 目标: tornado 实现多端口多进程运行   pip install supervisor echo_supervisord_conf > /etc/supervisord.conf   # 默认echo_supervisord_conf 在python/bin 目录下 配置/etc/supervisord.conf command=python /data/www/app/app.py --port=600%(process_num)d process_name=%(program_name)s%(process_num)d numprocs=6         ; 开启6个子进程 numprocs_start=1   ; 上面的process_num从1开始  directory=/data/www/app autostart=true autorestart=false ;startsecs=5                   ; number of secs prog must stay running (def. 1) startretries=3                ; max # of serial start failures (default 3) ;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=/data/logs/supervisor/order.log        ; stdout log path, NONE for none; default AUTO stderr_logfile=/data/logs/supervisor/order_error.log        ; stderr log path, NONE for none; default AUTO ;serverurl=AUTO                ; override serverurl computation (childutils)</code>
    转载请注明原文地址: https://ju.6miu.com/read-13239.html

    最新回复(0)