1. 程式人生 > >centos6.5安裝配置supervisor

centos6.5安裝配置supervisor

數量 started img processes demon var 後臺 not car

1.下載並安裝supervisor

https://pypi.python.org/pypi/supervisor/3.2.0

tar -zxvf supervisor-3.2.0.tar.gz
cd supervisor-3.2.0
python setup.py install

2.測試supervisord是否安裝成功

技術分享圖片
[root conf.d]# echo_supervisord_conf
; 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=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
;username=user              ; (default is no username (open server))
;password=123               ; (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.
技術分享圖片

3.建立文件夾,把應用的配置文件單獨放置

mkdir -p /opt/app/supervisor

mkdir -p /opt/app/supervisor/conf.d

4.創建默認的配置文件,並修改配置

技術分享圖片
echo_supervisord_conf >/opt/app/supervisor/supervisord.conf

vi /etc/supervisord.conf

[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))

[include]
files = ./conf.d/*.conf
技術分享圖片

5.設定supervisor啟動文件

技術分享圖片
vi /etc/init.d/supervisord

#! /usr/bin/env bash
# chkconfig: - 85 15

PATH=/sbin:/bin:/usr/sbin:/usr/bin

PROGNAME=supervisord

DAEMON=/usr/bin/$PROGNAME

CONFIG=/opt/app/supervisor/$PROGNAME.conf

PIDFILE=/tmp/$PROGNAME.pid

DESC="supervisord daemon"

SCRIPTNAME=/etc/init.d/$PROGNAME

# Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0


start()

{

echo -n "Starting $DESC: $PROGNAME"

$DAEMON -c $CONFIG

echo ".............start success"

}

stop()

{

echo "Stopping $DESC: $PROGNAME"

if [ -f "$PIDFILE" ];
then
supervisor_pid=$(cat $PIDFILE)
kill -15 $supervisor_pid
echo "......"
echo "stop success"
else
echo "$DESC: $PROGNAME is not Runing"
echo ".........................stop sucess"
fi
}

status()

{ statusport=`netstat -lntp|grep 9001|awk -F ‘ ‘ ‘{print $4}‘|awk -F ‘:‘ ‘{print $2}‘`

if [ -f "$PIDFILE" ];
then 
supervisor_pid=$(cat $PIDFILE)
echo "$DESC: $PROGNAME is Runing pid=$supervisor_pid"
else
echo "$DESC: $PROGNAME is not Runing"
echo "please use command /etc/init.d/supervisord start Run the service"
fi
}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

status)

status

;;

*)

echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2

exit 1

;;

esac

exit 0
技術分享圖片

添加為自啟動、啟動、關閉

chkconfig supervisord  on //添加自啟動服務

service supervisord start  //啟動服務

service supervisord stop   //停止服務

6.增加自定義的後臺進程

vi /opt/app/supervisor/conf.d/salt-minion.conf
[program:salt-minion]
command=salt-minion
autostart=true
autorestart=true


以後可以按照每個應用分別寫一個配置文件。

6.1(program)配置模板

技術分享圖片
[program:cat]
command=/bin/cat
process_name=%(program_name)s
numprocs=1
directory=/tmp
umask=022
priority=999
autostart=true
autorestart=true
startsecs=10
startretries=3
exitcodes=0,2
stopsignal=TERM
stopwaitsecs=10
user=chrism
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
技術分享圖片

6.2簡化模板

技術分享圖片
[program:test]
command=python test.py
directory=/home/supervisor_test/
autorestart=true
stopsignal=INT
user=root
stdout_logfile=test_out.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=test_err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
技術分享圖片

6.3(program)配置說明;*為必須填寫項;*[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) //信號量參考文章(http://c.biancheng.net/cpp/html/2784.html)
;掛起: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
技術分享圖片

7.啟動supervisor服務,並觀察salt-minion服務狀態

技術分享圖片
2016-06-12 17:39:57,643 INFO stopped: salt-minion (terminated by SIGTERM)
2016-06-12 17:39:57,866 CRIT Supervisor running as root (no user in config file)
2016-06-12 17:39:57,866 WARN Included extra file "/opt/app/supervisor/./conf.d/salt-minion.conf" during parsing
2016-06-12 17:39:57,928 INFO RPC interface ‘supervisor‘ initialized
2016-06-12 17:39:57,929 INFO RPC interface ‘supervisor‘ initialized
2016-06-12 17:39:57,929 CRIT Server ‘unix_http_server‘ running without any HTTP authentication checking
2016-06-12 17:39:57,931 INFO daemonizing the supervisord process
2016-06-12 17:39:57,931 INFO supervisord started with pid 18939
2016-06-12 17:39:58,938 INFO spawned: ‘salt-minion‘ with pid 18940
2016-06-12 17:39:59,940 INFO success: salt-minion entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
技術分享圖片

8.命令使用

Supervisor 有兩個可執行程序 — supervisord 和 supervisorctl:
supervisord 是後臺管理服務器, 用來依據配置文件的策略管理後臺守護進程;
supervisorctl 用於管理員向後臺管理程序發送 啟動/重啟/停止 等指令;
它們之間的關系就相當於 Apache 的 httpd 和 apachectl.

8.1查看所有命令

技術分享圖片
[root~]# supervisorctl help

default commands (type help <topic>):
=====================================
add exit open reload restart start tail 
avail fg pid remove shutdown status update 
clear maintail quit reread signal stop version
技術分享圖片

supervisord,初始啟動Supervisord,啟動、管理配置中設置的進程。
supervisorctl stop programxxx,停止某一個進程(programxxx),programxxx為[program:chatdemon]裏配置的值
,這個示例就是chatdemon。
supervisorctl start programxxx,啟動某個進程
supervisorctl restart programxxx,重啟某個進程
supervisorctl stop groupworker: ,重啟所有屬於名為groupworker這個分組的進程(start,restart同理)
supervisorctl stop all,停止全部進程,註:start、restart、stop都不會載入最新的配置文件。
supervisorctl reload,載入最新的配置文件,停止原有進程並按新的配置啟動、管理所有進程。
supervisorctl update,根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重
啟。
註意:顯示用stop停止掉的進程,用reload或者update都不會自動重啟。


8.2進入命令行

技術分享圖片
[root ~]# supervisorctl
Server requires authentication
Username:user
Password:

salt-minion RUNNING pid 18940, uptime 16:11:41
supervisor> help

default commands (type help <topic>):
=====================================
add exit open reload restart start tail 
avail fg pid remove shutdown status update 
clear maintail quit reread signal stop version

#顯示某個命令的用法 supervisor> help add add <name> [...] Activates any updates in config for process/group supervisor> help reload reload Restart the remote supervisord. #停止某個進程 supervisor> stop publisher_for_summary publisher_for_summary: stopped #查看此時此刻的狀態 supervisor> status publisher_for_detail RUNNING pid 27557, uptime 0:05:41 publisher_for_detail_nt RUNNING pid 27567, uptime 0:05:41 publisher_for_summary STOPPED Feb 27 02:48 PM publisher_for_summary_nt RUNNING pid 27568, uptime 0:05:41 worker_for_detail_all RUNNING pid 27581, uptime 0:05:41 worker_for_detail_recent RUNNING pid 27582, uptime 0:05:41 worker_for_summary RUNNING pid 27559, uptime 0:05:41 #發現被supervisorctl停掉的進程不會被自動重啟 #開啟剛才停掉的進程 supervisor> start publisher_for_summary publisher_for_summary: started supervisor> status publisher_for_detail RUNNING pid 27557, uptime 0:08:02 publisher_for_detail_nt RUNNING pid 27567, uptime 0:08:02 publisher_for_summary RUNNING pid 3035, uptime 0:00:04 publisher_for_summary_nt RUNNING pid 27568, uptime 0:08:02 worker_for_detail_all RUNNING pid 27581, uptime 0:08:02 worker_for_detail_recent RUNNING pid 27582, uptime 0:08:02 worker_for_summary RUNNING pid 27559, uptime 0:08:02 #停掉所有進程 supervisor> stop all worker_for_detail_recent: stopped worker_for_detail_all: stopped publisher_for_summary_nt: stopped publisher_for_detail_nt: stopped publisher_for_summary: stopped worker_for_summary: stopped publisher_for_detail: stopped supervisor> status publisher_for_detail STOPPED Feb 27 02:51 PM publisher_for_detail_nt STOPPED Feb 27 02:51 PM publisher_for_summary STOPPED Feb 27 02:51 PM publisher_for_summary_nt STOPPED Feb 27 02:51 PM worker_for_detail_all STOPPED Feb 27 02:51 PM worker_for_detail_recent STOPPED Feb 27 02:51 PM worker_for_summary STOPPED Feb 27 02:51 PM #開啟所有進程 supervisor> start all publisher_for_detail: started worker_for_summary: started publisher_for_summary: started publisher_for_detail_nt: started publisher_for_summary_nt: started worker_for_detail_all: started worker_for_detail_recent: started supervisor> status publisher_for_detail RUNNING pid 5111, uptime 0:00:15 publisher_for_detail_nt RUNNING pid 5141, uptime 0:00:15 publisher_for_summary RUNNING pid 5135, uptime 0:00:15 publisher_for_summary_nt RUNNING pid 5147, uptime 0:00:15 worker_for_detail_all RUNNING pid 5153, uptime 0:00:15 worker_for_detail_recent RUNNING pid 5159, uptime 0:00:14 worker_for_summary RUNNING pid 5112, uptime 0:00:15
技術分享圖片

9.http://10.129.62.42:9001/ 可以查看進程狀態

技術分享圖片

參考文檔:

http://www.linuxidc.com/Linux/2012-11/73832.htm
http://supervisord.org/configuration.html
http://www.cnblogs.com/yjf512/archive/2012/03/05/2380496.html
http://my.oschina.net/crooner/blog/395069
http://jwch.sdut.edu.cn/book/server/supervisor.html
http://www.2cto.com/os/201503/378878.html
http://c.biancheng.net/cpp/html/2784.html

centos6.5安裝配置supervisor