在AWS AMI Linux服务器上设置Supervisord

24
我正在尝试使用supervisor,以确保我的队列系统始终运行。以下是我按照不同来源整理的步骤:(请以root或超级用户身份运行)。
1) 运行命令: `$ easy_install supervisor`
2) 运行命令: `$ echo_supervisord_conf > /etc/supervisord.conf`
3) 运行命令: `$ sudo vi supervisord.conf`
4) 在文件末尾粘贴以下内容:
command=/usr/bin/php /path/to/AppName/artisan --env=production --timeout=240 queue:listen
5) 运行命令: `$ supervisord -c /etc/supervisord.conf`
6) 运行命令: `$ supervisorctl`
7) 输入命令: `status`,你会看到类似 `supervisor> status` 的输出
如果没有显示任何内容,请确认以上步骤是否正确执行,以及应用程序路径是否正确。

1
很难,但Ubuntu更容易。 - Yevgeniy Afanasyev
2个回答

51

以下是我采用的解决方案。AWS AMI 包含了 pip,可用于安装 Python 应用程序。以下是设置命令:

Here is the solution I went with. AWS AMI includes pip for installing Python applications. Here are the setup commands:

$ sudo pip install supervisor
$ echo_supervisord_conf
$ sudo su -
$ echo_supervisord_conf > /etc/supervisord.conf
安装完Supervisor后,您需要手动构建启动脚本以打开和关闭服务。这将因Linux发行版而异,Ubuntu在安装时会为您创建一个init脚本,而像AMI这样的其他发行版则不会。这是适用于各种Linux发行版init-up脚本的一个很好的资源:https://github.com/Supervisor/initscripts
然后,您可以将supervisor添加到chkconfig中,以便在系统重新启动时自动启动。这是适用于我的脚本之一: 路径
/etc/init.d/supervisord

AWS-AMI或RedHat Linux的示例初始化脚本

#!/bin/bash
#
# supervisord   Startup script for the Supervisor process control system
#
# 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
#               Erwan Queffelec <erwan.queffelec@gmail.com>
#                   make script LSB-compliant
#
# chkconfig:    345 83 04
# description: Supervisor is a client/server system that allows \
#   its users to monitor and control a number of processes on \
#   UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
#   its users to monitor and control a number of processes on
#   UNIX-like operating systems.
### END INIT INFO

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

# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
    . /etc/sysconfig/supervisord
fi

# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=/usr/local/bin/supervisorctl
supervisord=${SUPERVISORD-/usr/local/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/tmp/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
RETVAL=0

start() {
    echo -n $"Starting $prog: "
    daemon --pidfile=${pidfile} $supervisord $OPTIONS
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        touch ${lockfile}
        $supervisorctl $OPTIONS status
    fi
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    LSB=1 killproc -p $pidfile $supervisord -HUP
    RETVAL=$?
    echo
    if [ $RETVAL -eq 7 ]; then
        failure $"$prog reload"
    else
        $supervisorctl $OPTIONS status
    fi
}

restart() {
    stop
    start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${pidfile} $supervisord
        RETVAL=$?
        [ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
        ;;
    restart)
        restart
        ;;
    condrestart|try-restart)
        if status -p ${pidfile} $supervisord >&/dev/null; then
          stop
          start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
        RETVAL=2
    esac

    exit $RETVAL

在关闭并保存后,将其设置为所有用户可执行:

chmod a+x /etc/init.d/supervisord
你接下来需要确认 supervisord 进程是否在运行,可以通过运行以下命令进行确认:
 ps -fe | grep supervisor
如果您没有看到 /usr/bin/supervisord 作为正在运行的进程,则需要手动启动它:
sudo service supervisord start

每当服务器重新启动时,都需要启动Supervisord。这可以类似于使用chkconfig在重启后启动apache。

首先将其添加到chkconfig中,您的启动进程列表中。

sudo chkconfig --add supervisord

然后告诉chkconfig在启动后将其打开

sudo chkconfig supervisord on

我创建了“/etc/init.d/supervisord”文件,然后运行“sudo service supervisord start”,但是收到“env:/etc/init.d/supervisord:Permission denied”的错误消息...有什么想法吗?我正在使用带有CentOS 7的EC2 Amazon AIM。 - llioor
1
在这个非常详细的答案中,缺少了一个步骤,即chmod a+x /etc/init.d/supervisord以使其可执行。 - Filipe Pina
谢谢Filipe,我会更新答案。 - zeros-and-ones
@ llioor 尝试更改 /tmp/supervisor.sock 的所有者 - zeros-and-ones

7

supervisor不知道您已添加了一个程序。服务器故障上已回答此问题,请按以下步骤操作:

supervisorctl reread
supervisorctl update

顺便说一下,使用conf.d语法更容易维护配置文件。换句话说,创建一个名为/etc/supervisor/conf.d/artisan.conf的文件即可。其他都一样,但是使用这种方法更容易进行版本控制,并在设置时将它们同步到机器上。


嘿,感谢您的快速回复!我迫不及待地想试一下这个! - zeros-and-ones
我尝试过了,但出现了一些错误。我注意到当以su身份运行$ service supervisord start时,无法启动supervisor,会提示supervisor:未识别的服务。我需要像这样的启动脚本吗?http://translate.google.com/translate?hl=en&sl=ja&u=http://d.hatena.ne.jp/massat/20111013/1318484253&prev=search - zeros-and-ones
你是如何安装Supervisor的?我猜你在使用AWS Linux AMI?(编辑你的原始问题,添加这些内容,然后在此处添加评论。我会尝试复制。) - tedder42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接