zoukankan      html  css  js  c++  java
  • 使用 supervisor 管理进程

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

    Supervisor 可以运行在 Linux、Mac OS X 上。如前所述,supervisor 是 Python 编写的,所以安装起来也很方便,可以直接用 pip :

    sudo pip install supervisor
    或
    apt-get install supervisor

    如果是 Ubuntu 系统,还可以使用 apt-get 安装。

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

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

    echo_supervisord_conf > /etc/supervisord.conf

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

    文件路径如下: /etc/supervisor

    ; supervisor config file
    
    [unix_http_server]
    file=/var/run/supervisor.sock   ; (the path to the socket file)
    chmod=0700                       ; sockef file mode (default 0700)
    
    [supervisord]
    logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
    pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
    childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
    
    ; 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:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket
    
    ; 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/supervisor/conf.d/*.conf

    每个项目配置文件路径如下

    通常放到项目中通过软连接的方式到以下目录 ln -s cplgame.supervisor.conf  /etc/supervisor/conf.d

    /etc/supervisor/conf.d

    改变文件权限 不然会出现软连接失效的情况

    chmod 644 cplgame.supervisor.conf    lrwxrwxrwx

    [program:cplgame]
    command=/usr/local/bin/uwsgi -i uwsgi_config.ini              ; the program (relative uses PATH, can take args)
    process_name=%(program_name)s      ; process_name expr (default %(program_name)s)
    directory=/home/op/cplgame/run      ; 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=2                 ; max num secs to wait b4 SIGKILL (default 10)
    stopasgroup=true               ; send stop signal to the UNIX process group (default false)
    killasgroup=true               ; 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_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)

    常用命令

    装载
    supervisorctl reload
    
    查看状态
    supervisorctl status
    
    
    
    重启
    sudo supervisorctl restart cplgame


    supervisorctl status:查看所有进程的状态
    supervisorctl stop es:停止es
    supervisorctl start es:启动es
    supervisorctl restart es: 重启es
    supervisorctl update :配置文件修改后可以使用该命令加载新的配置
    supervisorctl reload: 重新启动配置中的所有程序

    踩过的坑
    1、unix:///var/run/supervisor/supervisor.sock no such file

         问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错

         解决办法:supervisord -c /etc/supervisord.conf 

    2、command中指定的进程已经起来,但supervisor还不断重启

         问题描述:command中启动方式为后台启动,导致识别不到pid,然后不断重启,本人使用的是elasticsearch,command                        指定的是$path/bin/elasticsearch -d,踩到的坑

         解决办法:supervisor无法检测后台启动进程的pid,而supervisor本身就是后台启动守护进程,因此不用担心这个

    3、启动了多个supervisord服务,导致无法正常关闭服务

        问题描述:在运行supervisord -c /etc/supervisord.conf 之前,我直接运行过supervisord -c /etc/supervisord.d/xx.conf                         ,导致有些进程被多个superviord管理,无法正常关闭进程。

        解决办法: 使用 ps -fe | grep supervisord 查看所有启动过的supervisord服务,kill相关的进程。

  • 相关阅读:
    struts2的核心和工作原理
    Java操作redis【二十】
    优化【十九】
    管线【十八】
    服务器管理【十七】
    虚拟内存【十六】
    Eclipse:使用findBugs预先检测错误
    linux命令学习(1)
    Android中Linux suspend/resume流程
    Ubuntu12.04下eclipse提示框黑色背景色的修改方法
  • 原文地址:https://www.cnblogs.com/zhaoyingjie/p/10494631.html
Copyright © 2011-2022 走看看