supervisor管理

一. Server端

  1. 修改pip源
    ```shell
    mkdir ~/.pip
    vim ~/.pip/pip.confweb

    [global]
    index-url = http://pypi.douban.com/simple
    ```
  2. 安装supervisor
    shell sudo pip install supervisor echo_supervisord_conf > /etc/supervisord.conf
  3. 去除里面大部分注释和“不相关”的部分,咱们能够先看这些配置:
    ```shell
    [unix_http_server]
    file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用
    ;chmod=0700 ; socket 文件的 mode,默认是 0700
    ;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gidshell

    ;[inet_http_server] ; HTTP 服务器,提供 web 管理界面
    ;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,若是开放到公网,须要注意安全性
    ;username=user ; 登陆管理后台的用户名
    ;password=123 ; 登陆管理后台的密码vim

    [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 的方式链接 supervisordapp

    ; 包含其余的配置文件
    [include]
    files = relative/directory/.ini ; 能够是 .conf 或 *.ini
    ```
  4. 咱们把上面这部分配置保存到 /etc/supervisord.conf(或其余任意有权限访问的文件),而后启动 supervisord(经过 -c 选项指定配置文件路径,若是不指定会按照这个顺序查找配置文件:$CWD/supervisord.conf, $CWD/etc/supervisord.conf, /etc/supervisord.conf):
    c supervisord -c /etc/supervisord.conf # 查看 supervisord 是否在运行: ps aux | grep supervisordsocket

二. Client端

  1. 为了举例,咱们新建一个目录 /etc/supervisor/ 用于存放这些配置文件,相应的,把 /etc/supervisord.conf 里 include 部分的的配置修改一下:
    shell [include] files = /etc/supervisor/*.conf
  2. 如今编写一份配置文件来管理进程(须要注意:用 supervisord 管理时,gunicorn 的 daemon 选项须要设置为 False):
    ```shell
    [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.logui

    ; 能够经过 environment 来添加须要的环境变量,一种常见的用法是修改 PYTHONPATH
    ; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere
    ```url

  3. 使用 supervisorctl
    ```shell
    supervisorctl -c /etc/supervisord.conf命令行

    $ supervisorctl status $ supervisorctl stop usercenter $ supervisorctl start usercenter $ supervisorctl restart usercenter $ supervisorctl reread $ supervisorctl update ```

相关文章
相关标签/搜索