supervisor守护进程

有时候写个脚本跑着跑着挂了,用supervisor守护进程挺方便的。
Supervisor是一个Python开发的client/server系统,可以通过pip安装,需要注意的是Python2.7以下兼容性很差,很可能运行失败。

pip install Supervisor 

安装好后

echo_supervisord_conf > /etc/supervisord.conf 

生成配置文件
然后我们在配置文件末尾追加想守护的进程即可,写法如下

[program:program-name]
command = python /root/program-dir/program-name.py 
user = root  
autostart = true  
autorestart = true  

配置好后可以执行supervisord启动,也可以killall -HUP supervisord
再然后我们添加supervisord到系统服务中
下面是Centos 示例代码

#!/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/init.d/functions

RETVAL=0
prog="supervisord"
pidfile="/tmp/supervisord.pid"
lockfile="/var/lock/subsys/supervisord"

start()
{
        echo -n $"Starting $prog: "
        daemon --pidfile $pidfile supervisord -c /etc/supervisord.conf
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch ${lockfile}
}

stop()
{
        echo -n $"Shutting down $prog: "
        killproc -p ${pidfile} /usr/bin/supervisord
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
                rm -f ${lockfile} ${pidfile}
        fi
}

case "$1" in

  start)
    start
  ;;

  stop)
    stop
  ;;

  status)
        status $prog
  ;;

  restart)
    stop
    start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|status}"

放在/etc/rc.d/init.d/supervisord 目录中,设置好执行权限后 我们chkconfig supervisord on 设置开机启动即可
简单吧!

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据