loading...

How to continuously run a Laravel queue listener on server

santhoshj profile image Santhosh ・2 min read

Laravel has a built-in queue management system which will help the main processing thread to ward off time taking asynchronous processes to a separate thread, also it can help the application to be scaled horizontally by adding multiple listeners/processors to simultaneously consume the queue.

In a development enviornment, we can listen to a queue by simply running

php artisan queue:listen

Where as, when we deploy this to a server, we cannot use this command. Because when we close the session, artisan command will exit. Earlier days, I was using screen command on linux to keep it running. Somehow, screen is a messy way to handle this situation.

To avoid this, we need to use a supervisor application to keep the artisan command alive.

There are many tools available to this purpose. A few of them are: forever, hypervisor, and pm2.

Here I am sharing the way how you can run artisan command using pm2.

pm2 start artisan --name QueueName --interpreter php -- queue:work --daemon

Explaining this, pm2 start will initialize artisan command. PM2 can start a named instance using --name, here QueueName will be the name of the instance. By default, commands will be run using node interpreter. So, --interpreter will set it to php. Finally, 'queue:work --daemon' is the argument to the php artisan command.

After running the above command from the laravel project root, run the following command to check the status.

pm2 list

PM2 screen grab

Status must be online. If it is not, run

pm2 monit

to see the logs and take corrective actions.

Written with StackEdit.

Discussion

pic
Editor guide