Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep Laravel Queue system running on server

I recently setup a Laravel Queue system. The basics are a cronjob calls a command which adds jobs to a queue and calls a second command which sends an email.

The system works when I ssh into my server and run php artisan queue:listen, but if I close my terminal the listener shuts down and the jobs stack up and sit in queue until I ssh back in and run listen again.

What is the best way to keep my queue system running in the background without needing to keep my connection open via ssh?

I tried running php artisan queue:work --daemon, and it completed the jobs in the queue, but when I closed my terminal it closed the connection and the background process.

like image 584
zeros-and-ones Avatar asked Feb 20 '15 06:02

zeros-and-ones


People also ask

How do I know if Laravel queue is running?

This is lower level but in the same vein you could run a command such as ps -aux | grep queue to literally view the running queue processes on whatever server your application/queue workers are running on.

What is Laravel queue sync?

Sync, or synchronous, is the default queue driver which runs a queued job within your existing process. With this driver enabled, you effectively have no queue as the queued job runs immediately.

What does php artisan queue listen do?

queue:listen will listen to the queue, and continue to process any queue commands it receives. This will continue running indefinitely until you stop it. In Laravel >=4.2 there was a --daemon command added.


1 Answers

Running

nohup php artisan queue:work --daemon & 

Will prevent the command exiting when you log out.

The trailing ampersand (&) causes process start in the background, so you can continue to use the shell and do not have to wait until the script is finished.

See nohup

nohup - run a command immune to hangups, with output to a non-tty

This will output information to a file entitled nohup.out in the directory where you run the command. If you have no interest in the output you can redirect stdout and stderr to /dev/null, or similarly you could output it into your normal laravel log. For example

nohup php artisan queue:work --daemon > /dev/null 2>&1 &  nohup php artisan queue:work --daemon > app/storage/logs/laravel.log & 

But you should also use something like Supervisord to ensure that the service remains running and is restarted after crashes/failures.

like image 67
Ben Swinburne Avatar answered Oct 05 '22 11:10

Ben Swinburne