Linux supervisor 安装与使用

1,安装:pip install supervisor

2,生成配置文件:echo_supervisord_conf > /etc/supervisord.conf

3,编辑配置文件:vim /etc/supervisord.conf

修改最后两行,去掉前面的分号。

[include]
files = /etc/supervisord/*.conf

这是存放进被管理的进程的配置文件

4,开启web控制台:找到如下几行,去掉注释

[inet_http_server]   ; inet (TCP) server disabled by default
port=0.0.0.0:9001   ; ip_address:port specifier, *:port for all iface
username=admin   ; default is no username (open server)
password=123456   ; default is no password (open server)
 
之后可以通过web界面登录管理,查看日志等
 
5,添加管理进程的配置文件
mkdir /etc/supervisord/
vim /etc/supervisord/test1.conf
[program:test1]
user=root
directory=/usr/local/test/
command=/usr/local/bin/python /usr/local/test/test.py
autostart=true
autorestart=true
loglevel=debug
log_stderr=true
stdout_logfile=/var/log/test1.log
redirect_stderr=true

如需管理多个进程,那就配置多个文件

6,启动superv

supervisord -c /etc/supervisord.conf
 
7, 查看管理进程的状态
supervisorctl status  (被管理的进程会随着supervisor主进程的启动而启动)
 
8,关闭,启动,重启
supervisorctl stop|start|restart test1
 
9,关闭supervisor
ps -ef | grep super,然后kill -9 杀进程
 
10,注意,直接杀掉supervisor的主进程,被管理的进程仍然运行状态,所以记得先stop被管理的进程。
如果修改了各个配置文件,都需要重启supervisor。

补充:添加supervisord服务,设置开机启动

上面安装的没有生成supervisord这个服务,自己参考改一个

文件如下:vim /etc/init.d/supervisord

#!/bin/sh
##
## /etc/rc.d/init.d/supervisord
##
#supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord

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

prog="supervisord"
prefix="/usr/local/"
exec_prefix="${prefix}"
PIDFILE="/var/run/supervisord.pid"
CONFIG="/etc/supervisord.conf"
prog_bin="${exec_prefix}bin/supervisord -c $CONFIG "

function log_success_msg() {
        echo "$@" "[ OK ]"
}

function log_failure_msg() {
        echo "$@" "[ OK ]"
}

start()
{
       #echo -n $"Starting $prog: "
       #daemon $prog_bin --pidfile $PIDFILE
       #[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog failed"
       #echo
        if [ ! -r $CONFIG ]; then
                log_failure_msg "config file doesn't exist (or you don't have permission to view)"
                exit 4
        fi

        if [ -e $PIDFILE ]; then
                PID="$(pgrep -f $PIDFILE)"
                if test -n "$PID" && kill -0 "$PID" &>/dev/null; then
                        # If the status is SUCCESS then don't need to start again.
                        log_failure_msg "$NAME process is running"
                        exit 0
                fi
        fi

        log_success_msg "Starting the process" "$prog"
        daemon $prog_bin --pidfile $PIDFILE
        log_success_msg "$prog process was started"

}
stop()
{
       echo -n $"Shutting down $prog: "
       [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
       echo
}

case "$1" in

 start)
   start
 ;;

 stop)
   stop
 ;;

 status)
       status $prog
 ;;

 restart)
   stop
   start
 ;;

 *)
   echo "Usage: $0 {start|stop|restart|status}"
 ;;

esac

文件中的prefix和config变量记得检查下

给文件执行权限

chmod -R 755 /etc/init.d/supervisord

添加服务

chkconfig --add supervisord

设置开机启动

chkconfig  supervisord on

测试:

测试前先把之前启动的supervisord进程和子进程杀掉,kill -9

然后

service supervisord start

service supervisord stop

管理的子进程也会一并起来和杀死

原文地址:https://www.cnblogs.com/cq90/p/9538048.html