Palzin Track
Get 15% off with code PTRACKSIGNUP15 

Laravel Diary Logo

How to Set-up Supervisor on EC2 Server

tooling
Table of Contents

Here are the steps I went with.

## first get epel

sudo amazon-linux-extras install epel

## then install supervisor
sudo yum install supervisor

echo_supervisord_conf
sudo su -
echo_supervisord_conf > /etc/supervisord.conf

After you install supervisor you will need to manually build your start-up script to turn the service on and off.

This will vary with your Linux distro, Ubuntu will create an init script for you when you install, other distros like AMI will not. Here is a great resource for various Linux distro init-up scripts:

Supervisor Init Scripts

You can then add supervisor to chkconfig to get started automatically on system reboot.

Here is one that works for me:

Path

/etc/init.d/supervisord

Example Init Script for AWS-AMI or RedHat Linux


#!/bin/bash
#
## supervisord   Startup script for the Supervisor process control system
#
## Author:       Mike McGrath <[email protected]> (based off yumupdatesd)
##               Jason Koppe <[email protected]> adjusted to read sysconfig,
##                   use supervisord tools to start/stop, conditionally wait
##                   for child processes to shutdown, and startup later
##               Erwan Queffelec <[email protected]>
##                   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

After you close and save, make it executable by all users:

chmod a+x /etc/init.d/supervisord

You would next want to confirm that the supervisord process is in fact running by running the following command:

ps -fe | grep supervisor

If you don't see /usr/bin/supervisord as a running process then you need to start it up manually:

sudo service supervisord start

Supervisord needs to be started up anytime that the server is rebooted. This can be done similar to how apache is turned on after reboot using chkconfig.

First add it to chkconfig, your start up process list

sudo chkconfig --add supervisord

Then tell chkconfig to turn it on after boot

sudo chkconfig supervisord on

Go to supervisor config directory cd /etc/supervisor/conf.d

Now let us create a new laravel configuration $ sudo nano laravel-worker.conf

Paste following contents to our file

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app.com/artisan queue:work sqs --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/app.com/worker.log

Make sure in above file to change the location of your project directory by replacing /var/www/app.com and to replace user with your linux logged in user id.

Once we saved the contents we need to run following command to initialize new configurations:

## read the new config
$ sudo supervisorctl reread

## activate our configuration
$ sudo supervisorctl update

## start queue command
$ sudo supervisorctl start laravel-worker:*

## check the status of our new config
$ sudo supervisorctl status

That is it, we are all setup with supervisor. Our next task will be to create a new job and push that job into database. Our supervior will pick up this new job and process it in the background.

::Share it on::

Comments (0)

What are your thoughts on "How to Set-up Supervisor on EC2 Server"?

You need to create an account to comment on this post.

Related articles