利用Supervisor来守护我们自己的进程

Supervisor

安装supervisor

  • 利用python的pip或者easy_install来安装
easy_install supervisor
或者
pip installsupervisor

生成默认配置文件

echo_supervisord_conf > /etc/supervisord.conf

启动准备

  • 创建配置文件来存放自己的多个配置文件
mkdir /etc/supervisord.conf.d
  • 修改默认配置
#include区段修改为
[include]
files =/etc/supervisord.conf.d/*.conf
  • 网页访问
#如需要访问web控制界面,inet_http_server区段修改为
[inet_http_server]
port=0.0.0.0:9001
username=username ; 你的用户名
password=password ; 你的密码
  • 注意事项
  1. 每个需要管理的进程分别写在一个文件里面,放在/etc/supervisord.conf.d/目录下,便于管理。例如:test.conf

将supervisord加入系统服务

vim /etc/init.d/supervisord

  • 启动脚本如下
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
# Source init functions
. /etc/init.d/functions
RETVAL=0
prog="supervisord"
pidfile="/tmp/supervisord.pid"
lockfile="/var/lock/subsys/supervisord"
start()
{       
        echo -n $"Starting $prog: "
        daemon --pidfile $pidfile supervisord -c /etc/supervisord.conf
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop()
{       
        echo -n $"Shutting down $prog: "
        killproc -p ${pidfile} /usr/bin/supervisord
        RETVAL=$?
        echo 
        if [ $RETVAL -eq 0 ] ; then
                rm -f ${lockfile} ${pidfile}
        fi
}
case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  status)
        status $prog
  ;;
  restart)
    stop
    start
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
  ;;
esac
  • 添加执行权限
chmod +x /etc/init.d/supervisord
  • 启动
chkconfig supervisordon
service supervisord start

配置文件实例,主要为末尾部分

      ; 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".
        ;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

        [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))

        ;[inet_http_server]         ; inet (TCP) server disabled by default
        ;port=0.0.0.0:9001    #修改可以访问的ip地址,一般为全部可以访问      ; (ip_address:port specifier, *:port for all iface)
        ;username=root              ; (default is no username (open server))
        ;password=test              ; (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           ; (num of main logfile rotation backups;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)
        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 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 ; 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 http_username if set
        ;password=123                ; should be same as http_password if set
        ;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
        ;history_file=~/.sc_history  ; use readline history if available

        ; The below sample program section 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,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=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 (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)

        ; The below sample eventlistener section shows all possible
        ; eventlistener subsection values, create one or more 'real'
        ; eventlistener: sections to be able to handle event notifications
        ; sent by supervisor.

        ;[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,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=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 (default 10)
        ;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_events_enabled=false   ; emit events on stderr writes (default false)
        ;environment=A="1",B="2"       ; process environment additions
        ;serverurl=AUTO                ; override serverurl computation (childutils)

        ; The below sample group section 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 = files = /etc/supervisord.conf.d/*.conf  ##修改的地方
        #添加以下内容在第一个节点上,其他节点只需要最后的;storm supervisor
        ; storm nimbus
        [program:storm-nimbus]
        command=/data/dmp/storm/bin/nimbus-start.sh
        directory=/data/dmp/storm
        autorestart=true
        autostart=true
        startsecs=5
        startretries=20
        stopsignal=QUIT
        user=root

        ; storm ui
        [program:storm-ui]
        command=/data/dmp/storm/bin/ui-start.sh
        directory=/data/dmp/storm
        autorestart=true
        autostart=true
        startsecs=5
        startretries=20
        user=root

        ;storm supervisor
        [program:storm-supervisor]
        command=/data/dmp/storm/bin/supervisor-start.sh
        directory=/data/dmp/storm
        autorestart=true
        autostart=true
        startsecs=5
        startretries=20
        user=root

配置文件说明

;*为必须填写项
;*[program:应用名称]
[program:cat]

;*命令路径,如果使用python启动的程序应该为 python /home/test.py, 
;不建议放入/home/user/, 对于非user用户一般情况下是不能访问
command=/bin/cat

;当numprocs为1时,process_name=%(program_name)s;
当numprocs>=2时,%(program_name)s_%(process_num)02d
process_name=%(program_name)s

;进程数量
numprocs=1

;执行目录,若有/home/supervisor_test/test1.py
;将directory设置成/home/supervisor_test
;则command只需设置成python test1.py
;否则command必须设置成绝对执行目录
directory=/tmp

;掩码:--- -w- -w-, 转换后rwx r-x w-x
umask=022

;优先级,值越高,最后启动,最先被关闭,默认值999
priority=999

;如果是true,当supervisor启动时,程序将会自动启动
autostart=true

;*自动重启
autorestart=true

;启动延时执行,默认1秒
startsecs=10

;启动尝试次数,默认3次
startretries=3

;当退出码是0,2时,执行重启,默认值0,2
exitcodes=0,2

;停止信号,默认TERM
;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐)
;终止:TERM(kill -TERM pid)
;挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同
;从容停止:QUIT(kill -QUIT pid)
;KILL, USR1, USR2其他见命令(kill -l),说明1
stopsignal=TERM

stopwaitsecs=10

;*以root用户执行
user=root

;重定向
redirect_stderr=false

stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB

;环境变量设置
environment=A="1",B="2"

serverurl=AUTO

常用信号说明

信号名称 数字表示 说明
SIGHUP 1 终端挂起或控制进程终止。当用户退出Shell时,由该进程启动的所有进程都会收到这个信号,默认动作为终止进程。
SIGINT 2 键盘中断。当用户按下组合键时,用户终端向正在运行中的由该终端启动的程序发出此信号。默认动作为终止进程。
SIGQUIT 3 键盘退出键被按下。当用户按下或组合键时,用户终端向正在运行中的由该终端启动的程序发出此信号。默认动作为退出程序。
SIGFPE 8 发生致命的运算错误时发出。不仅包括浮点运算错误,还包括溢出及除数为0等所有的算法错误。默认动作为终止进程并产生core文件。
SIGKILL 9 无条件终止进程。进程接收到该信号会立即终止,不进行清理和暂存工作。该信号不能被忽略、处理和阻塞,它向系统管理员提供了可以杀死任何进程的方法。
SIGALRM 14 定时器超时,默认动作为终止进程。
SIGTERM 15 程序结束信号,可以由 kill 命令产生。与SIGKILL不同的是,SIGTERM 信号可以被阻塞和终止,以便程序在退出前可以保存工作或清理临时文件等。
原文地址:https://www.cnblogs.com/forsaken627/p/6511325.html