Fucking pain in the ass. I remember 1 year ago, this process used to be super straight forward and easy.
Anyway with Laravel 5.5 and Amazon Linux in December 2017, here is what I did to make it work.
Install Supervisor
1 |
sudo easy_install supervisor |
Create supervisor OS config file. This is DIFFERENT from the Supervisor config that runs your process(es). This is for initialising Supervisor in Linux and allow it to be run as a service.
1 2 3 |
cd /etc/init.d touch supervisor |
Edit supervisor and enter the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
#!/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 |
Change permissions to allow it to be read and executed.
1 |
chmod a+x /etc/init.d/supervisord |
Check if process is running
1 |
ps -fe | grep supervisor |
Try running it
1 |
sudo service supervisord start |
For some reason, my service became supervisor instead of supervisord
Create Supervisor Config file for laraval worker.
1 |
touch /etc/supervisord.conf |
Paste this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[supervisord] nodaemon=true [supervisorctl] [inet_http_server] port = 127.0.0.1:9001 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /WEBSITE_PATH/artisan queue:work --sleep=3 --tries=3 autostart=true autorestart=true user=<USER> numprocs=2 redirect_stderr=true stdout_logfile=/WEBSITE_PATH/worker.log |
Run supervisord to see if its working. For some reason mine only worked in sudo if i put the full path, i.e. /usr/local/bin/supervisord
The command is:
1 |
supervisord -c /etc/supervisord.conf |
If i’m not wrong running supervisord will also automatically load the file.
LASTLY
Add service to your start up workers.
1 |
sudo chkconfig --add supervisord |
1 |
sudo chkconfig supervisord on |
Restart your motherfucking server, and it should be running by default.
References:
https://stackoverflow.com/questions/28702780/setting-up-supervisord-on-a-aws-ami-linux-server