1. Supervisor是一個C/S系統,它能夠在類unix操做系統讓用戶來監視和控制後臺服務進程的數量,一個很重要的功能就是監控服務器的主要後臺進程,並在出現問題是自動重啓。html
2.
根據服務器上的python版本下載對應的setuptools
Python 2.6.6
wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086
直接安裝
sh setuptools-0.6c11-py2.6.egg
|
3.
下載並安裝supervisor
wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0b1.tar.gz
tar -zxvf supervisor-3.0b1.tar.gz
cd supervisor-3.0b1
python setup.py install
安裝setuptools後也能夠
easy_install supervisor
|
4.
設定supervisor配置文件
建立默認的配置文件
echo_supervisord_conf >/etc/supervisord.conf
vi /etc/supervisord.conf
取消如下的註釋,並修改IP爲0.0.0.0
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0: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))
增長自定義的後臺進程(注意進程名之間用一個:分隔)
[program:hello]
command=python /root/hello.py
priority=1
numprocs=1
autostart=true
autorestart=true
startretries=10
stopsignal=KILL
stopwaitsecs=10
redirect_stderr=true
stdout_logfile=/root/hello.log
|
5.
設定supervisor啓動文件
vi /etc/init.d/supervisord
#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PROGNAME=supervisord
DAEMON=/usr/bin/$PROGNAME
CONFIG=/etc/$PROGNAME.conf
PIDFILE=/tmp/$PROGNAME.pid
DESC="supervisord daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -n "Starting $DESC: $PROGNAME"
$DAEMON -c $CONFIG
echo "..."
}
stop()
{
echo -n "Stopping $DESC: $PROGNAME"
supervisor_pid=$(cat $PIDFILE)
kill -15 $supervisor_pid
echo "..."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
|
6.
寫一個簡單的python腳本
安裝web.py
easy_install web.py
vi /root/hello.py
import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
|
7.
啓動supervisor服務,並觀察hello服務狀態
/etc/init.d/supervisord start
查看日誌
tail -f /tmp/supervisord.log
2012-11-09 15:33:53,367 WARN received SIGTERM indicating exit request
2012-11-09 15:33:53,367 INFO waiting for :hello to die
2012-11-09 15:33:53,371 INFO stopped: :hello (terminated by SIGKILL)
2012-11-09 15:33:53,514 CRIT Supervisor running as root (no user in config file)
2012-11-09 15:33:53,552 INFO RPC interface 'supervisor' initialized
2012-11-09 15:33:53,552 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2012-11-09 15:33:53,560 INFO daemonizing the supervisord process
2012-11-09 15:33:53,562 INFO supervisord started with pid 13698
2012-11-09 15:33:54,568 INFO spawned: ':hello' with pid 13699
2012-11-09 15:33:55,572 INFO success: :hello entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
能夠使用supervisorctl管理進程
upervisorctl status 查詢狀態
supervisorctl start hello 開啓服務
supervisorctl stop hello 關閉服務
|
8.
以前配置文件開啓了web訪問,這樣能夠直接經過瀏覽器觀察和控制進程,很是方便
參考資料