Supervisor安装与配置(Linux下)

注:Supervisor目前只能运行在python2.x
一、安装
1.1、使用apt-get安装

$ sudo apt-get install supervisor

1.2、使用pip安装

$ sudo pip install  supervisor

安装之后:会产生三个可执行程序,supervisord supervisorctl echo_supervisor_conf,这三个程序分别是:守护程序、supervisor客户端、创建默认supervisor的配置文件。
二、配置
2.1、使用apt-get安装后的配置
对于使用apt-get1安装成功后,默认的配置都已经生成,配置文件路径为:

$ /etc/supervisor
该目录下所有文件:
$ ls
conf.d  supervisord.conf
其中supervisord.conf是supervisord的配置文件
conf.d是一个目录,我们可以将我们的进程配置移到改目录下
分文件组织

配置文件的内容为:

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; 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:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; 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 = /etc/supervisor/conf.d/*.conf  

不用修改就可以正常使用。
2.2、使用pip安装后的配置
使用pip安装后没有任何默认的配置生成,所以直接运行supervisord会失败,一版错误提示是,没有找到配置文件。


2.2.1、创建supervisor的配置目录,我们模仿1.1中的配置目录结构创建

$ sudo mkdir -p /etc/supervisor/conf.d

2.2.2、使用echo_supervisor_conf生成默认配置

$ echo_supervisor_conf > supervisored.conf  可以将这个文件随便生成在用户自己的目录,然后使用sudo拷贝到/etc/supervisor/目录下
$ sudo cp supervisored.conf /etc/supervisor

然后我们打开supervisored.conf配置看一下:

[unix_http_server]
;file=/tmp/supervisor.sock   ; (the path to the socket file)
;避免被系统删除, 可以模仿2.1中的该项配置
file=/home/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)
;可以模仿2.1中的该项配置
logfile=/var/log/supervisor/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)
;可以模仿2.1中的该项配置
pidfile=/home/supervisor/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
...
;设置启动supervisord的用户,一般情况下不要轻易用root用户来启动,所以我们可以注释掉,使用普通用户来启动,这里注意,如果我们没有上面模仿2.1中的配置创建的文件或者文件夹,要使用chown将文件所属修改为当前用户。
;user=chrism                 ; (default is current user, required if root)
...
[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;可以模仿2.1中的该项配置
serverurl=unix:///home/supervisor/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

配置后可以启动supervisord

supervisord -c /etc/supervisor/supervisord.conf

三、使用Supervisor管理进程

四、设置开机自启
如果使用apt-get安装的,supervisored就在开机自启动中(ubuntu安装完使用sysv-rc-conf --list查看)。
如果使用pip安装就需要手动设置。
4.1、编写service来控制supervisor daemon

$ cd /etc/init.d/
$ sudo touch supervisord
$ sudo vim supervisord

#!/bin/bash
#
# supervisord   Startup script for the Supervisor process control system
#
# Author:       Mike McGrath  (based off yumupdatesd)
#               Jason Koppe  adjusted to read sysconfig,
#                   use supervisord tools to start/stop, conditionally wait
#                   for child processes to shutdown, and startup later
#               Erwan Queffelec 
#                   make script LSB-compliant
#
# chkconfig:    345 83 04
# description: Supervisor is a client/server system that allows \
#   its users to monitor and control a number of processes on \
#   UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
#   its users to monitor and control a number of processes on
#   UNIX-like operating systems.
### END INIT INFO

# Source function library
. /etc/rc.d/init.d/functions

# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
    . /etc/sysconfig/supervisord
fi

# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=/usr/local/bin/supervisorctl
supervisord=${SUPERVISORD-/usr/local/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/tmp/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
# 这是supervisor的配置文件所在目录
OPTIONS="${OPTIONS--c /etc/supervisor/supervisord.conf}"
RETVAL=0

start() {
    echo -n $"Starting $prog: "
    daemon --pidfile=${pidfile} $supervisord $OPTIONS
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        touch ${lockfile}
        $supervisorctl $OPTIONS status
    fi
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    LSB=1 killproc -p $pidfile $supervisord -HUP
    RETVAL=$?
    echo
    if [ $RETVAL -eq 7 ]; then
        failure $"$prog reload"
    else
        $supervisorctl $OPTIONS status
    fi
}

restart() {
    stop
    start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${pidfile} $supervisord
        RETVAL=$?
        [ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
        ;;
    restart)
        restart
        ;;
    condrestart|try-restart)
        if status -p ${pidfile} $supervisord >&/dev/null; then
          stop
          start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
        RETVAL=2
    esac

    exit $RETVAL

完成之后加上可执行权限

$ sudo chmod a+x supervisord

这个时候我们就可以使用下面命令管理Supervisor

sudo service supervisord start
sudo service supervisord stop
...

4.2、设置开机自启
对于centos上可以使用chkconfig添加

sudo chkconfig --add supervisord
sudo chkconfig supervisord on

如果想移除可以使用

chkconfig --help

你可能感兴趣的:(Supervisor安装与配置(Linux下))