superviserでとりあえずデーモン化 その1

Supervisorをインストールすると,比較的お気軽になんでもデーモン化できます.

インストール方法は以下のとおり.

$ sudo pip install supervisor

CentOSでsupervisorが起動時に自動起動できるように,スクリプトを作成します.

$ sudo nano /etc/init.d/supervisord
#!/bin/sh
#
# description: supervisord - this script starts and stops the supervisord daemon
#
# chkconfig: 345 99 01
#
# processname:  supervisord
# daemon: supervisord
# config: /etc/supervisord.conf

# import functions
. /etc/rc.d/init.d/functions

RETVAL=0
pythonpath=/usr/local/python27/bin/python
superviserd=/usr/local/python27/bin/supervisord
superviserctl=/usr/local/python27/bin/supervisorctl
configfile=/etc/supervisord.conf
prog=supervisord
pidfile=/var/run/supervisor/supervisord.pid
lockfile=/var/lock/subsys/supervisord

start() {
    echo -n $"Starting $prog: "
    if [ -e $pidfile ]; then
        echo $( passed )
        return 1
    fi

    # Start supervisord whith python path
    daemon $pythonpath $superviserd -c $configfile --pidfile $pidfile
    RETVAL=$?
    echo

    # if succsesed to run supervisord,  do this.
    if [ $RETVAL=0 ]; then
        # show status
        $pythonpath $superviserctl -c $configfile status
        #  created the PIDFILE
        touch $lockfile
     fi

     echo
     return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    # kill supervisord
    killproc -p $pidfile $supervisord  -c $configfile -QUIT
    RETVAL=$?
    echo

    # remove lockfile
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    return $RETVAL
}

restart() {
        stop
        start
}

reload () {
    echo -n $"Reloading $prog: "
    killproc -p $pidfile $supervisord -c $configfile -HUP
    RETVAL=$?
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    status)
        status -p ${pidfile} supervisord
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload}"
        exit 1
esac

exit $RETVAL

作成した起動スクリプトを動かしてみて,問題が無ければ時度起動するように設定します.

$ sudo service supervisord status
$ sudo service supervisord start
$ sudo chkconfig --add supervisord
$ sudo chkconfig supervisord on

これで,インストールは完了です.

Webから操作できるように設定する方は後日.