欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

安装supervisord 和开机自启动 有大用

本文参考了 http://www.tuicool.com/articles/Ejm2u2 http://www.cnblogs.com/kaituorensheng/p/5020793.html安装supervisord 
Pip install  supervisord  #如果不好下载就去官网下载vim /etc/supervisord.conf

supervisor手动启动:

#/usr/bin/supervisord -c /etc/supervisor.conf

echo_supervisord_conf > /etc/supervisord.conf

修改配置文件:

supervisord.conf最后增加(分号后边的表示注释,可以不写)

[program:bandwidth]

command=python26 /usr/local/bin/bandwidth.sh  ;需要执行的命令wd)

user =root  ;(default  is  current  user , required  if  root)

autostart=true  ;start at supervisord start (default: true)

autorestart=true  ;whether/when to restart (default: unexpected)

startsecs=3  ;number of secs prog must stay running ( def . 1)

stderr_logfile=/tmp/bandwidth_err.log  ;redirect proc stderr to stdout (default false) 错误输出重定向

stdout_logfile=/tmp/bandwidth.log  ;stdout log path, NONE  for  none; default AUTO

运行命令:

supervisord -c /etc/supervisord.conf  //启动supervisor

supervisorctl //打开命令行

ctl中: help //查看命令

ctl中: status //查看状态

另外如果修改了 /etc/supervisord.conf ,需要执行 supervisorctl reload 来重新加载配置文件,否则不会生效。


设置自启动方式:

vi /etc/init.d/supervisord

注意:该文件中的如下几个变量,都需要根据你实际的目录来改写。

PREFIX=/usr/local

SUPERVISORD=$PREFIX/bin/supervisord      ##supervisord   程序的安装路径

SUPERVISORCTL=$PREFIX/bin/supervisorctl  ##supervisorctl 程序的安装路径

PIDFILE=/var/supervisor/supervisord.pid   ##需要先创建/var/supervisor目录

LOCKFILE=/var/supervisor/supervisord.lock

OPTIONS="-c /etc/supervisord.conf"   ##配置文件的路径

增加以下内容:

#!/bin/bash

# supervisord   This scripts turns supervisord on

# chkconfig:    345 83 04

# description:  supervisor is a process control utility.  It has a web based

#                          xmlrpc interface as well as a few other nifty features.

#

 

# source function library

. /etc/rc.d/init.d/functions

 

set -a

PREFIX=/usr/local

 

SUPERVISORD= /usr/local/bin/supervisord

SUPERVISORCTL= /usr/local/bin/supervisorctl

 

PIDFILE=/var/supervisor/supervisord.pid

LOCKFILE=/var/supervisor/supervisord.lock

 

OPTIONS="-c /etc/supervisord.conf"

 

# unset this variable if you don't care to wait for child processes to shutdown before removing the $LOCKFILE-lock

WAIT_FOR_SUBPROCESSES=yes

 

# remove this if you manage number of open files in some other fashion

ulimit -n 96000

RETVAL=0

running_pid()

{

 

    # Check if a given process pid's cmdline matches a given name

    pid=$1

    name=$2

    [ -z "$pid" ] && return 1

    [ ! -d /proc/$pid ] && return 1

    (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1

    return 0

}

 

running()

{

# Check if the process is running looking at /proc

# (works for all users)

    # No pidfile, probably no daemon present

    [ ! -f "$PIDFILE" ] && return 1

    # Obtain the pid and check it against the binary name

    pid=`cat $PIDFILE`

    running_pid $pid $SUPERVISORD || return 1

    return 0           

}

 

 

 

start() {

    echo "Starting supervisord: "

 

    if [ -e $PIDFILE ]; then

    echo "ALREADY STARTED"

    return 1

    fi

 

  # start supervisord with options from sysconfig (stuff like -c)

    $SUPERVISORD $OPTIONS

 

    # show initial startup status

    $SUPERVISORCTL $OPTIONS status

 

    # only create the subsyslock if we created the PIDFILE

    [ -e $PIDFILE ] && touch $LOCKFILE

}

 

 

stop() {

    echo -n "Stopping supervisord: "

    $SUPERVISORCTL $OPTIONS shutdown

    if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then

        echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"

        for sleep in  2 2 2 2 4 4 4 4 8 8 8 8 last; do

            if [ ! -e $PIDFILE ] ; then

            echo "Supervisord exited as expected in under $total_sleep seconds"

            break

            else

                if [[ $sleep -eq "last" ]] ; then

                    echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"

                    return 1

                else

                    sleep $sleep

                    total_sleep=$(( $total_sleep + $sleep ))

                fi

            fi

        done

    fi

 

    # always remove the subsys. We might have waited a while, but just remove it at this point.

    rm -f $LOCKFILE

 }

 

 

restart() {

    stop

    start

}

 

 

case "$1" in

 

 start)

   start

 ;;

 

 stop)

   stop

 ;;

 

 status)

       status $prog

 ;;

 

 restart)

   stop

   start

 ;;

 

 *)

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

 ;;

 

esac

保存完毕之后,可以执行以下命令修改文件权限:

chmod 777 /etc/init.d/supervisord

/etc/init.d/supervisord  start

配置开机启动:

chkconfig supervisord  on

查看是否成功:

chkconfig --list | grep  supervisord 


来自  http://www.360doc.com/content/17/0619/18/17267365_664508603.shtml

普通分类: