Linux supervisor 安裝與使用

1,安裝:pip install supervisorpython

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

3,編輯配置文件:vim /etc/supervisord.confvim

修改最後兩行,去掉前面的分號。測試

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

這是存放進被管理的進程的配置文件spa

4,開啓web控制檯:找到以下幾行,去掉註釋debug

[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

如需管理多個進程,那就配置多個文件rest

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服務,設置開機啓動code

上面安裝的沒有生成supervisord這個服務,本身參考改一個server

文件以下: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

管理的子進程也會一併起來和殺死

相關文章
相關標籤/搜索