欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

centos6.5安装配置supervisor 有大用

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

欢迎加入python/Django群:475749546 喜欢读书的人可加生活.读书.新知群:475749546


来自  https://www.cnblogs.com/sfnz/p/5578417.html


普通分类: