supervisor使用

supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启。

根据服务器上的python版本下载对应的setuptools

[root@test1 ~]# python -V
Python
wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086
直接安装
sh setuptools-.6c11-py2..egg

下载并且安装supervisor

wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0b1.tar.gz
tar -zxvf supervisor-.0b1.tar.gz
cd supervisor-.0b1
python setup.py install

安装setuptools后也可以
easy_install supervisor

设定supervisor配置文件

创建默认的配置文件
echo_supervisord_conf  >/etc/supervisord.conf
vi /etc/supervisord.conf

取消以下的注释,并修改IP为0.
[inet_http_server]         ; inet (TCP) server disabled by default
port=        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=               ; (default is no password (open server))

增加自定义的后台进程(注意进程名之间用一个:分隔)
[program:hello]
command=python /root/hello.py
priority=
numprocs=
autostart=true
autorestart=true
startretries=
stopsignal=KILL
stopwaitsecs=
redirect_stderr=true
stdout_logfile=/root/hello.log

设定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 

start()
{
        echo -n "Starting $DESC: $PROGNAME"
        $DAEMON -c $CONFIG
        echo "..."
}
stop()
{
        echo -n "Stopping $DESC: $PROGNAME"
        supervisor_pid=$(cat $PIDFILE)
        kill - $supervisor_pid
        echo "..."
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo
        exit
        ;;
esac
exit 

写一个简单的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()

启动supervisor服务,并观察hello服务状态

/etc/init.d/supervisord start

查看日志
tail -f /tmp/supervisord.log
-- ::, WARN received SIGTERM indicating exit request
-- ::, INFO waiting for :hello to die
-- ::, INFO stopped: :hello (terminated by SIGKILL)
-- ::, CRIT Supervisor running as root (no user in config file)
-- ::, INFO RPC interface 'supervisor' initialized
-- ::, CRIT Server 'unix_http_server' running without any HTTP authentication checking
-- ::, INFO daemonizing the supervisord process
-- ::, INFO supervisord started with pid
-- ::, INFO spawned:
-- ::, INFO success: :hello entered RUNNING state, process has stayed up  seconds (startsecs)

可以使用supervisorctl管理进程
upervisorctl status        查询状态
supervisorctl start hello    开启服务
supervisorctl stop hello    关闭服务

之前配置文件开启了web访问,这样可以直接通过浏览器观察和控制进程,非常方便

supervisor使用

supervisor使用

参考:http://supervisord.org/

上一篇:winpcap usb山寨网卡识别


下一篇:ARM1