一陈惰、安裝
下載最新的supervisor安裝包:https://pypi.python.org/pypi/supervisor
cd /usr/local/src
wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz
tar -zxvf supervisor-3.3.2.tar.gz
cd supervisor-3.3.2
python setup.py install
Tips:錯(cuò)誤1:ImportError: No module named setuptools
## 下載pip
wget https://files.pythonhosted.org/packages/c2/f7/c7b501b783e5a74cf1768bc174ee4fb0a8a6ee5af6afa92274ff964703e0/setuptools-40.8.0.zip
unzip setuptools-40.8.0.zip
cd setuptools-40.8.0
python setup.py install
Tips:錯(cuò)誤2:error: Could not find suitable distribution for Requirement.parse(‘meld3>=0.6.5‘)畦徘,安裝meld擴(kuò)展
wget https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91
tar zxvf meld3-1.0.2.tar.gz
cd meld3-1.0.2
python setup.py install
Tips:本地python版本為python3以下
二、配置
1.生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
2.啟動(dòng)
supervisord -c /etc/supervisord.conf
##查看 supervisord 是否在運(yùn)行:
ps aux | grep supervisord
3.基礎(chǔ)配置
(1)打開配置文件
vim /etc/supervisord.conf
(2)在配置文件底部抬闯,配置include
[include]
files=/etc/supervisor/*.conf #若你本地?zé)o/etc/supervisor目錄井辆,請(qǐng)自建
(3)用supervisor管理進(jìn)程,配置如下:
cd /etc/supervisor
vim ossfs.conf # 這里的文件名稱自定義
(4)加入以下內(nèi)容:
; 設(shè)置進(jìn)程的名稱溶握,使用 supervisorctl 來管理進(jìn)程時(shí)需要使用該進(jìn)程名
[program:your_program_name]
command=python server.py --port=9000
;numprocs=1 ; 默認(rèn)為1
;process_name=%(program_name)s ; 默認(rèn)為 %(program_name)s杯缺,即 [program:x] 中的 x
directory=/home/python/tornado_server ; 執(zhí)行 command 之前,先切換到工作目錄
user=oxygen ; 使用 oxygen 用戶來啟動(dòng)該進(jìn)程
; 程序崩潰時(shí)自動(dòng)重啟睡榆,重啟次數(shù)是有限制的萍肆,默認(rèn)為3次
autorestart=true
redirect_stderr=true ; 重定向輸出的日志
stdout_logfile = /var/log/supervisord/tornado_server.log
loglevel=info
(5)這里是啟動(dòng)要配置的參數(shù),請(qǐng)根據(jù)自己的項(xiàng)目自定義添加
更改了supervisor配置文件胀屿,需要重啟,運(yùn)行以下指令:
supervisorctl reload
(6)服務(wù)啟動(dòng)
supervisord -c /etc/supervisord.conf
(7)用瀏覽器來管理進(jìn)程配置
打開配置文件
vim /etc/supervisord.conf
配置 inet_http_server
[inet_http_server]
port=127.0.0.1:9001; 服務(wù)器ip
username=username ;自定義
password=password ;自定義
三塘揣、supervisorctl的用法
supervisord : 啟動(dòng)supervisor
supervisorctl reload :修改完配置文件后重新啟動(dòng)supervisor
supervisorctl status :查看supervisor監(jiān)管的進(jìn)程狀態(tài)
supervisorctl start all | 進(jìn)程名 :?jiǎn)?dòng)全部或某進(jìn)程
supervisorctl stop all | 進(jìn)程名 :停止全部或某進(jìn)程
supervisorctl stop all:停止進(jìn)程,注:start宿崭、restart亲铡、stop都不會(huì)載入最新的配置文件。
supervisorctl update:根據(jù)最新的配置文件葡兑,啟動(dòng)新配置或有改動(dòng)的進(jìn)程奖蔓,配置沒有改動(dòng)的進(jìn)程不會(huì)受影響而重啟
四、設(shè)置開機(jī)啟動(dòng)
(1)添加開機(jī)腳本
vim /etc/init.d/supervisord
腳本如下:
#! /bin/sh
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Author: Dan MacKinlay <danielm@phm.gov.au>
# Based on instructions by Bertrand Mathieu
# http://zebert.blogspot.com/2009/05/installing-django-solr-varnish-and.html
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Description of the service"
NAME=supervisord
DAEMON=/usr/local/bin/supervisord
DAEMON_ARGS=" -c /etc/supervisord.conf"
#PIDFILE=/var/run/$NAME.pid
PIDFILE=/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
(2)設(shè)置該腳本為可以執(zhí)行
sudo chmod +x /etc/init.d/supervisord
(3)設(shè)置為開機(jī)自動(dòng)運(yùn)行
sudo update-rc.d supervisord defaults
(4)試一下讹堤,是否工作正常
service supervisord stop
service supervisord start
若報(bào)錯(cuò):insserv: warning: script 'service' missing LSB tags and overrides吆鹤,請(qǐng)執(zhí)行:
sudo apt-get remove insserv
五、錯(cuò)誤排查
1洲守、unix:///var/run/supervisor/supervisor.sock no such file
問題描述:安裝好supervisor沒有開啟服務(wù)直接使用supervisorctl報(bào)的錯(cuò)
解決辦法:?jiǎn)?dòng)服務(wù)
supervisord -c /etc/supervisord.conf
2疑务、啟動(dòng)了多個(gè)supervisord服務(wù),導(dǎo)致無法正常關(guān)閉服務(wù)
問題描述:多次運(yùn)行supervisord -c /etc/supervisord.conf 或supervisord岖沛,導(dǎo)致有些進(jìn)程被多個(gè)superviord管理暑始,無法正常關(guān)閉進(jìn)程。
解決辦法:kill相關(guān)的進(jìn)程婴削。
ps -fe | grep supervisord #查看所有啟動(dòng)過的supervisord服務(wù)
kill -9 PID #kill相關(guān)進(jìn)程
3、supervisord 啟動(dòng)失敗 Error: Another program is already listening on a port that one of our HTTP serve...
unlink /var/run/supervisor.sock
unlink /tmp/supervisor.sock
這個(gè)錯(cuò)誤的原因就是supervisor.sock 這個(gè)文件會(huì)被系統(tǒng)自動(dòng)刪除或者其它原因不存在了牙肝,刪除軟連接就可以了唉俗。 supervisor.sock 生成的位置可以去 supervisor 的配置文件中找到嗤朴。
轉(zhuǎn)載:http://www.reibang.com/p/3658c963d28b