利用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 信號能夠被阻塞和終止,以便程序在退出前能夠保存工做或清理臨時文件等。
相關文章
相關標籤/搜索