How to Run php artisan queue:work at all time

Run Laravel Queue Workers with Supervisor

  1. Supervisor can keep a Laravel queue worker running in the background and automatically restart it if the process stops.

Install Supervisor

  1. On Ubuntu:
sudo apt update
sudo apt install supervisor
  1. Check that Supervisor is running:
sudo systemctl status supervisor

Create the Laravel Worker Configuration

  1. Create a new Supervisor configuration file:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
  1. Add:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/your-project/artisan queue:work --sleep=3 --tries=3 --timeout=90
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/your-project/storage/logs/worker.log
stopwaitsecs=3600
  1. Replace:
/var/www/your-project
  1. with the actual Laravel project path.
  2. The user should also match the user that should run the Laravel application. On a typical Ubuntu web server this is often:
www-data

Load the Configuration

  1. After creating or changing the Supervisor configuration:
sudo supervisorctl reread
sudo supervisorctl update
  1. Start the worker:
sudo supervisorctl start laravel-worker
  1. Check its status:
sudo supervisorctl status
  1. You should see something similar to:
laravel-worker RUNNING

Restart the Worker

  1. If Laravel application code used by queued jobs has changed, restart the queue workers so they load the new code:
php artisan queue:restart
  1. Laravel allows the currently running job to finish and then restarts the worker safely.
  2. If the Supervisor configuration itself was changed, run:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart laravel-worker

Useful Supervisor Commands

sudo supervisorctl status
sudo supervisorctl start laravel-worker
sudo supervisorctl stop laravel-worker
sudo supervisorctl restart laravel-worker
  1. php artisan queue:restart is normally used after deploying Laravel code changes.

supervisorctl reread and supervisorctl update are required when the Supervisor configuration file itself has been changed.

Contact

Want to get in touch?

If you want to talk about a project, a software or IT role, something you have built, or a system you want to improve, send me a message.

I read the messages myself.

Thank you. Your message has been sent.