centos6.5安装配置supervisor

时间:2023-03-09 08:47:32
centos6.5安装配置supervisor

1.下载并安装supervisor

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

tar -zxvf supervisor-3.2..tar.gz
cd supervisor-3.2.
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= ; socket file mode (default )
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password= ; (default is no password (open server)) ;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1: ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password= ; (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= ; (num of main logfile rotation backups;default )
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= ; (min. avail startup file descriptors;default )
minprocs= ; (min. avail process descriptors;default )
;umask= ; (process file creation umask;default )
;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= ; 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= ; number of processes copies to start (def )
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask= ; umask for process (default None)
;priority= ; the relative start priority (default )
;autostart=true ; start at supervisord start (default: true)
;startsecs= ; # of secs prog must stay up to be running (def. )
;startretries= ; max # of serial start failures when starting (default )
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=, ; 'expected' exit codes used with autorestart (default ,)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs= ; max num secs to wait b4 SIGKILL (default )
;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= ; # of stdout logfile backups (default )
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default )
;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= ; # of stderr logfile backups (default )
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default )
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="",B="" ; 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= ; number of processes copies to start (def )
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size= ; event buffer queue size (default )
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask= ; umask for process (default None)
;priority=- ; the relative start priority (default -)
;autostart=true ; start at supervisord start (default: true)
;startsecs= ; # of secs prog must stay up to be running (def. )
;startretries= ; max # of serial start failures when starting (default )
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=, ; 'expected' exit codes used with autorestart (default ,)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs= ; max num secs to wait b4 SIGKILL (default )
;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= ; # of stdout logfile backups (default )
;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= ; # of stderr logfile backups (default )
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="",B="" ; 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= ; the relative start priority (default ) ; 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: ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password= ; (default is no password (open server))
[include]
files = ./conf.d/*.conf

5.设定supervisor启动文件

vi /etc/init.d/supervisord

#! /usr/bin/env bash
# chkconfig: - 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 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 - $supervisor_pid
echo "......"
echo "stop success"
else
echo "$DESC: $PROGNAME is not Runing"
echo ".........................stop sucess"
fi
} status() { statusport=`netstat -lntp|grep |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}" >& exit ;; esac exit

添加为自启动、启动、关闭

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=
directory=/tmp
umask=
priority=
autostart=true
autorestart=true
startsecs=
startretries=
exitcodes=,
stopsignal=TERM
stopwaitsecs=
user=chrism
redirect_stderr=false
stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=
stderr_capture_maxbytes=1MB
environment=A="",B=""
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=
stdout_capture_maxbytes=1MB
stderr_logfile=test_err.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=
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= ;执行目录,若有/home/supervisor_test/test1.py
;将directory设置成/home/supervisor_test
;则command只需设置成python test1.py
;否则command必须设置成绝对执行目录
directory=/tmp ;掩码:--- -w- -w-, 转换后rwx r-x w-x
umask= ;优先级,值越高,最后启动,最先被关闭,默认值999
priority= ;如果是true,当supervisor启动时,程序将会自动启动
autostart=true ;*自动重启
autorestart=true ;启动延时执行,默认1秒
startsecs= ;启动尝试次数,默认3次
startretries= ;当退出码是0,2时,执行重启,默认值0,
exitcodes=, ;停止信号,默认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= ;*以root用户执行
user=root ;重定向
redirect_stderr=false stdout_logfile=/a/path
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=
stderr_capture_maxbytes=1MB ;环境变量设置
environment=A="",B="" serverurl=AUTO

7.启动supervisor服务,并观察salt-minion服务状态

-- ::, INFO stopped: salt-minion (terminated by SIGTERM)
-- ::, CRIT Supervisor running as root (no user in config file)
-- ::, WARN Included extra file "/opt/app/supervisor/./conf.d/salt-minion.conf" during parsing
-- ::, INFO RPC interface 'supervisor' initialized
-- ::, INFO RPC interface 'supervisor' initialized
-- ::, CRIT Server 'unix_http_server' running without any HTTP authentication checking
-- ::, INFO daemonizing the supervisord process
-- ::, INFO supervisord started with pid
-- ::, INFO spawned: 'salt-minion' with pid
-- ::, INFO success: salt-minion entered RUNNING state, process has stayed up for > than 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 , uptime ::
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 , uptime ::
publisher_for_detail_nt RUNNING pid , uptime ::
publisher_for_summary STOPPED Feb : PM
publisher_for_summary_nt RUNNING pid , uptime ::
worker_for_detail_all RUNNING pid , uptime ::
worker_for_detail_recent RUNNING pid , uptime ::
worker_for_summary RUNNING pid , uptime ::
#发现被supervisorctl停掉的进程不会被自动重启 #开启刚才停掉的进程
supervisor> start publisher_for_summary
publisher_for_summary: started
supervisor> status
publisher_for_detail RUNNING pid , uptime ::
publisher_for_detail_nt RUNNING pid , uptime ::
publisher_for_summary RUNNING pid , uptime ::
publisher_for_summary_nt RUNNING pid , uptime ::
worker_for_detail_all RUNNING pid , uptime ::
worker_for_detail_recent RUNNING pid , uptime ::
worker_for_summary RUNNING pid , uptime :: #停掉所有进程
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 : PM
publisher_for_detail_nt STOPPED Feb : PM
publisher_for_summary STOPPED Feb : PM
publisher_for_summary_nt STOPPED Feb : PM
worker_for_detail_all STOPPED Feb : PM
worker_for_detail_recent STOPPED Feb : PM
worker_for_summary STOPPED Feb : 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 , uptime ::
publisher_for_detail_nt RUNNING pid , uptime ::
publisher_for_summary RUNNING pid , uptime ::
publisher_for_summary_nt RUNNING pid , uptime ::
worker_for_detail_all RUNNING pid , uptime ::
worker_for_detail_recent RUNNING pid , uptime ::
worker_for_summary RUNNING pid , uptime ::

9.http://10.129.62.42:9001/ 可以查看进程状态

centos6.5安装配置supervisor

参考文档:

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