supervisor安装及配置——管理守护进程的福利

CentOS7以上直接使用yum安装即可正常使用小配文件配置supervisor程序
并可以多进程方式启动自己的进守护进程
但CentOS6系统使用yum安装的supervisor不支持使用小配置文件和多进程
所以要想在CentOS6系统上的正常使用supervisor管理自己的守护进程
就需要使用python安装器进行安装,具体步骤参考如下

 
1,安装python安装器
yum install -y python-setuptools
 
2,使用python安装器安装supervisor
easy_install supervisor
 
3,查看supervisor版本
supervisord --version
 
4,生成supervisor默认配置文件
echo_supervisord_conf > /etc/supervisord.conf
 
5,有效配置内容参考如下
 
[unix_http_server]
file=/tmp/supervisor.sock //如果修改成其它目录,需要确认进程的属主对其目录有写权限
[supervisord]
logfile=/data/log/supervisor/supervisord.log //修改成自定义的日志目录,需要确认进程的属主对其目录有写权限
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/tmp/supervisord.pid //如果修改成其它目录,需要确认进程的属主对其目录有写权限
nodaemon=false
minfds=1024
minprocs=200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock //如果file选项有做过修改,此处需要对应
[include] //默认状态小配文件没有打开,需要自动取消注释
files = supervisord.d/*.conf //此处的官方建议使用相对路径(相对于/etc目录)
 
6,生成服务启动脚本以便使用service命令进行管理
vim /etc/rc.d/init.d/supervisord
 
#!/bin/bash
 

chkconfig: - 55 45
description: supervisor service
processname: supervisord
config: /etc/supervisord.conf
pidfile: /var/run/supervisord.pid

 
. /etc/init.d/functions
 
DAEMON=/usr/bin/supervisord
PIDFILE=/var/run/supervisord.pid
 
[ -x "$DAEMON" ] || exit 0
 
start() {
echo -n "Starting supervisord: "
if [ -f $PIDFILE ]; then
PID=cat $PIDFILE
echo supervisord already running: $PID
exit 2;
else
daemon $DAEMON --pidfile=$PIDFILE -c /etc/supervisord.conf
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
return $RETVAL
fi

}
 
stop() {
echo -n "Shutting down supervisord: "
echo
killproc -p $PIDFILE supervisord
echo
rm -f /var/lock/subsys/supervisord
return 0
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status supervisord
;;
restart)
stop
start
;;
*)
echo "Usage: {start|stop|status|restart}"

  • exit 1
    ;;
    esac
    exit $?
     
    7、给脚本添加执行权限
    chmod +x /etc/rc.d/init.d/supervisord
     
    8,将上面生成的启动脚本添加进CentOS服务管理器
    chkconfig --add supervisord
     
    9、将supervisor服务设成自启动
    chkconfig supervisord on
     
    10、添加自己需要守护的进程的小配文件
    vim /etc/supervisord.d/self.conf
     
    具体格式参考官方给出的默认版本,也即如下这一部分
    ;[program:theprogramname]
    ;command=/bin/cat ; the program (relative uses PATH, can take args)
    ;processname=%(programname)s ; processname expr (default %(programname)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
    ;redirectstderr=true ; redirect proc stderr to stdout (default false)
    ;stdoutlogfile=/a/path ; stdout log path, NONE for none; default AUTO
    ;stdoutlogfilemaxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
    ;stdoutlogfilebackups=10 ; # of stdout logfile backups (0 means none, default 10)
    ;stdoutcapturemaxbytes=1MB ; number of bytes in 'capturemode' (default 0)
    ;stdouteventsenabled=false ; emit events on stdout writes (default false)
    ;stderrlogfile=/a/path ; stderr log path, NONE for none; default AUTO
    ;stderrlogfilemaxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
    ;stderrlogfilebackups=10 ; # of stderr logfile backups (0 means none, default 10)
    ;stderrcapturemaxbytes=1MB ; number of bytes in 'capturemode' (default 0)
    ;stderreventsenabled=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 sample eventlistener section below shows all possible eventlistener
    ; subsection values. Create one or more 'real' eventlistener: sections to be
    ; able to handle event notifications sent by supervisord.
     
    ;[eventlistener:theeventlistenername]
    ;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
    ;processname=%(programname)s ; processname expr (default %(programname)s)
    ;numprocs=1 ; number of processes copies to start (def 1)
    ;events=EVENT ; event notif. types to subscribe to (req'd)
    ;buffersize=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
    ;redirectstderr=false ; redirectstderr=true is not allowed for eventlisteners
    ;stdoutlogfile=/a/path ; stdout log path, NONE for none; default AUTO
    ;stdoutlogfilemaxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
    ;stdoutlogfilebackups=10 ; # of stdout logfile backups (0 means none, default 10)
    ;stdouteventsenabled=false ; emit events on stdout writes (default false)
    ;stderrlogfile=/a/path ; stderr log path, NONE for none; default AUTO
    ;stderrlogfilemaxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
    ;stderrlogfilebackups=10 ; # of stderr logfile backups (0 means none, default 10)
    ;stderreventsenabled=false ; emit events on stderr writes (default false)
    ;environment=A="1",B="2" ; process environment additions
    ;serverurl=AUTO ; override serverurl computation (childutils)
     
    在这里给出一个自己在用的简化实例
    [program:jumpserver]
    command =source /opt/py3/bin/activate && /data/jumpserver/jms start all -d
    processname = %(programname)s%(processnum)s
    numprocs = 1
    autostart= true
    autorestart= true
    stdoutlogfile=/data/log/jumpserver/jumpserver.log
     
    11、一切准备好之后就可以启动supervisord服务了
    如上配置的话,supervisord进程启动后,其所管理的子进程也会自动被启动
     
    至此为此,一个简单的supervisor安装加配置基本上搞定了
    想使用supervisor官理自己的守护进程的同仁们可以大胆开工了!*

猜你喜欢

转载自blog.51cto.com/183530300/2154265