The official Laravel docs mention Supervisor as a preferred process manager to manage queue workers. For my requirements Upstart proved itself capable of handling the job just as well.
The Laravel documentation suggests Supervisor to solve two problems. First, we need to start the queue workers automatically in the background after the system has booted. Secondly we have to make sure that the process will be restarted if it exits unexpectedly.
Upstart is the default init system on Ubuntu and can meet both requirements out of the box. Because it’s already installed there is no need to grab extra packages.
(Note: Systemd has become the default system init daemon since Ubuntu 16, instead of upstart)
All it takes is the Upstart job file. Create the file
/etc/init/laravel.conf and add the following lines:
description “My Queue Worker“ author "Steven Rombauts" start on startup stop on shutdown respawn setuid deploy setgid deploy chdir /var/www/myapp/ exec php artisan queue:work
This config file explains itself rather well: it will start the service on startup, and stop when the system shuts down. The
respawn directive ensures the command will be started again if it exits.
The key line here is the
exec line, which tells Upstart what command to run. In our case we need
php artisan queue:work.
setgid to change to another user and group before running the command. Change both lines to the appropriate values.
chdir directive should point to your Laravel application. Upstart will set the working directory to this path before starting the job.
We can now start the service:
sudo service laravel start
Look in the process list to verify the queue worker is running:
ps -aef | grep queue
Finally, let’s double check that the process will be restarted automatically. Kill the running queue worker:
pkill -f "php artisan queue:work"
ps -aef | grep queue again and you should see the queue worker being listed with a new process ID.
We’re good to go, time to dispatch some jobs!