Issue
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.
Solution
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.
Answered By - Ben Swinburne Answer Checked By - Terry (WPSolving Volunteer)