php artisan queue:work stops when I close SSH, and after deploying new code the worker still runs old code. How do I configure Supervisor properly?
DEV3 Developer Forum
How do I keep a Laravel queue worker running with Supervisor and restart it on deploy?
Ask a question
Every post is reviewed first. Usually reviewed within 24 hours.
dev3 ·
0
1 reply
dev3 · Accepted answer
Queue workers are long-running PHP processes that boot the framework once, so they keep old code in memory until restarted. Supervisor keeps them alive, and queue:restart makes them reload after deploys.
Install: sudo apt install supervisor. Create /etc/supervisor/conf.d/laravel-worker.conf:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/app/storage/logs/worker.log
stopwaitsecs=3600
Load it:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
On every deploy run:
php artisan queue:restart
This signals workers to exit after finishing the current job; Supervisor starts fresh ones with the new code. It relies on the cache store to carry the signal, so the cache driver must be shared by CLI and workers (not array).
Tips
--max-time=3600makes workers recycle hourly to limit memory growth.stopwaitsecsmust exceed your longest job, or Supervisor kills it mid-way.--triesandfailed_jobstable: usephp artisan queue:failedandqueue:retry all.- Use
queue:work, notqueue:listen(slower). - Laravel Horizon is optional and Redis-only; Supervisor is enough for database or SQS queues.
An alternative is a systemd unit with Restart=always for the same command.
0