CentOS6.9安装Supervisor管理后台进程并设置开机启动

本篇笔记记录了CentOS6.9中安装Supervisor管理后台进程,并将Supervisord加入服务,设置开机启动,通过service管理的过程

创建并进入源码存放目录

mkdir -p /usr/local/src
cd /usr/local/src

下载并解压supervisor源码

wget -c https://files.pythonhosted.org/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz
tar -zxvf supervisor-3.3.5.tar.gz
cd supervisor-3.3.5

安装supervisor

python setup.py install

如果出现错误:ImportError: No module named setuptools
安装python-setuptoolsyum install python-setuptools
下面的结果表示supervisor安装成功

Installed /usr/lib/python2.6/site-packages/meld3-1.0.2-py2.6.egg
Finished processing dependencies for supervisor==3.3.5

创建supervisord.d目录

mkdir /etc/supervisord.d

创建并编辑配置文件

echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf

修改以下几处

file=/var/run/supervisor.sock

logfile=/var/log/supervisord.log

pidfile=/var/run/supervisord.pid

serverurl=unix:///var/run/supervisor.sock

[include]
files = supervisord.d/*.conf

启动supervisord

supervisord -c /etc/supervisord.conf

查看进程

ps aux | grep supervisord
root       3110  0.0  1.0 196612 10816 ?        Ss   20:12   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root       3504  0.0  0.0 103336   908 pts/0    S+   20:19   0:00 grep supervisord
kill 3110

创建启动脚本

vim /etc/init.d/supervisord

写入如下配置

#!/bin/bash
#
# supervisord   This scripts turns supervisord on
#
# Author:       Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
#               Jason Koppe <jkoppe@indeed.com> adjusted to read sysconfig,
#                   use supervisord tools to start/stop, conditionally wait
#                   for child processes to shutdown, and startup later
#               Mikhail Mingalev <mingalevme@gmail.com> Merged
#                   redhat-init-jkoppe and redhat-sysconfig-jkoppe, and
#                   made the script "simple customizable".
#               Brendan Maguire <maguire.brendan@gmail.com> Added OPTIONS to
#                   SUPERVISORCTL status call
#
# 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.
#               Script was originally written by Jason Koppe <jkoppe@indeed.com>.
#

# source function library
. /etc/rc.d/init.d/functions

set -a

PREFIX=/usr

SUPERVISORD=$PREFIX/bin/supervisord
SUPERVISORCTL=$PREFIX/bin/supervisorctl

PIDFILE=/var/run/supervisord.pid
LOCKFILE=/var/lock/subsys/supervisord

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
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    restart|force-reload)
        restart
        RETVAL=$?
        ;;
    reload)
        $SUPERVISORCTL $OPTIONS reload
        RETVAL=$?
        ;;
    condrestart)
        [ -f $LOCKFILE ] && restart
        RETVAL=$?
        ;;
    status)
        $SUPERVISORCTL $OPTIONS status
        if running ; then
            RETVAL=0
        else
            RETVAL=1
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
        exit 1
esac

exit $RETVAL

加入服务

chkconfig --add supervisord

设置权限

chmod 755 /etc/init.d/supervisord

开机启动

chkconfig supervisord on

创建测试php文件

vim /www/test.php

写入如下代码,让进程启动时,先休眠10秒,然后写入log后立即退出

<?php
sleep(10);
error_log(date('Y-m-d H:i:s').":".getmypid()."\n",3,'/var/log/test.php.log');

创建项目进程管理配置

vim /etc/supervisord.d/jmsite.conf

写入如下配置

[program:jmsite]
command=/usr/bin/php /www/test.php
process_name=%(program_name)s_%(process_num)02d
autorestart=true
autostart=true
numprocs=3
startsecs=1
stderr_logfile=/var/log/supervisor.err.log
stdout_logfile=/var/log/supervisor.out.log

启动supervisor

service supervisord start
Starting supervisord: 
jmsite:jmsite_00                 STARTING  
jmsite:jmsite_01                 STARTING  
jmsite:jmsite_02                 STARTING

查看supervisor运行状态

service supervisord status
jmsite:jmsite_00                 RUNNING   pid 7096, uptime 0:01:38
jmsite:jmsite_01                 RUNNING   pid 7097, uptime 0:01:38
jmsite:jmsite_02                 RUNNING   pid 7098, uptime 0:01:38

查看supervisor日志

tail -f /var/log/supervisord.log 
2019-01-27 21:45:42,031 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:48,022 INFO exited: jmsite_00 (exit status 0; expected)
2019-01-27 21:45:49,025 INFO spawned: 'jmsite_00' with pid 8105
2019-01-27 21:45:50,023 INFO exited: jmsite_01 (exit status 0; expected)
2019-01-27 21:45:51,026 INFO success: jmsite_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:51,027 INFO spawned: 'jmsite_01' with pid 8106
2019-01-27 21:45:51,045 INFO exited: jmsite_02 (exit status 0; expected)
2019-01-27 21:45:52,046 INFO success: jmsite_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:52,047 INFO spawned: 'jmsite_02' with pid 8107
2019-01-27 21:45:53,050 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:59,040 INFO exited: jmsite_00 (exit status 0; expected)
2019-01-27 21:46:00,043 INFO spawned: 'jmsite_00' with pid 8111
2019-01-27 21:46:01,043 INFO success: jmsite_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:46:01,045 INFO exited: jmsite_01 (exit status 0; expected)
2019-01-27 21:46:02,048 INFO spawned: 'jmsite_01' with pid 8112
2019-01-27 21:46:02,063 INFO exited: jmsite_02 (exit status 0; expected)
2019-01-27 21:46:03,064 INFO success: jmsite_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:46:03,065 INFO spawned: 'jmsite_02' with pid 8113
2019-01-27 21:46:04,067 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
......

查看test.php产生的log

tail -f /var/log/test.php.log
2019-01-27 21:45:14:8052
2019-01-27 21:45:15:8053
2019-01-27 21:45:17:8087
2019-01-27 21:45:25:8088
2019-01-27 21:45:26:8089
2019-01-27 21:45:29:8092
2019-01-27 21:45:36:8093
2019-01-27 21:45:39:8096
2019-01-27 21:45:40:8097
2019-01-27 21:45:48:8100
2019-01-27 21:45:50:8101
2019-01-27 21:45:51:8102
2019-01-27 21:45:59:8105
2019-01-27 21:46:01:8106
2019-01-27 21:46:02:8107
......

通过以上日志我们看到,php的进程在写入log后即退出,而supervisor在php进程退出后会再次启动进程