DEV Community

Ankit Verma
Ankit Verma

Posted on

How to Set Up Laravel Queue Workers with Supervisor and Cron

πŸ“¦ What is Supervisor?
Supervisor is a Linux process monitor that keeps your Laravel queue workers running continuously and restarts them automatically if they fail.

⏰ What is Crontab?
Crontab is a Unix utility that runs scripts or commands at scheduled times. In Laravel, this is used for running scheduled tasks defined in your app/Console/Kernel.php in Laravel 10+ routes/console.php .

Before you start, make sure you have:

  • Laravel project set up and ready to use
  • Root or sudo access to the server
  • Your project path and domain correctly configured

Step 1: Install Supervisor

Install Supervisor on Ubuntu:

sudo apt-get install supervisor
sudo service supervisor restart
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Supervisor Configuration File

Open the configuration file for editing:

sudo vim /etc/supervisor/conf.d/laravel-worker.conf
Enter fullscreen mode Exit fullscreen mode

Paste the following configuration:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/yourdomain.com/artisan queue:work
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/yourdomain.com/worker.log
stopwaitsecs=3600
Enter fullscreen mode Exit fullscreen mode

Replace /var/www/html/yourdomain.com/ with the actual path to your Laravel project.

Step 3: Start Supervisor with Your Configuration

Reload Supervisor to recognize the new config and start the worker:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
Enter fullscreen mode Exit fullscreen mode

To check the worker status:

sudo supervisorctl status
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Up Laravel Scheduler with Cron

Open the crontab configuration:

crontab -e
Enter fullscreen mode Exit fullscreen mode

Add this line to run Laravel's scheduler every minute:

* * * * * cd /var/www/html/yourdomain.com && php artisan schedule:run >> /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

Replace the path with your actual Laravel app directory.

Optional: Uninstall Supervisor

If needed, you can remove Supervisor using:

sudo apt-get remove supervisor
sudo apt-get autoremove
Enter fullscreen mode Exit fullscreen mode

Questions or feedback? Drop a comment!

Top comments (2)

Collapse
 
andreitelteu profile image
Andrei Telteu

Please be careful when you use the user root in queues and cron job, but your website php-fpm runs under a different user ! (with log configuration daily)
If your queue or cron job writes something to the log file, if the log file does not exist for the current date, it creates it with the owner root.
When your website tries to write something to the log file (some debug log or anything), it crashes completely because if the website runs under a different user (like www-data) it does not have ownership on the log file and a critical error occurs because of a simple debug log.

Collapse
 
ankitvermaonline profile image
Ankit Verma

if you’re facing permission issues due to different users just change the user

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/yourdomain.com/artisan queue:work
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data ; <- changed from root to www-data
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/yourdomain.com/worker.log
stopwaitsecs=3600

Some comments may only be visible to logged-in visitors. Sign in to view all comments.