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
管理的子進程也會一並起來和殺死
