Skip to content

Laravel queue on shared hosting: run queue:work from cron

· The AYT team · 5 min read

Developer desk with two monitors, one showing code

On shared hosting you usually can't keep php artisan queue:work running permanently, because the host stops long-running processes. The usual way to run a Laravel queue on shared hosting: keep jobs in the database (QUEUE_CONNECTION=database) and have the scheduler start queue:work --stop-when-empty every minute, which processes whatever it finds and then exits. Jobs start with a delay of at most a minute. If your host offers supervised processes, a permanent worker is still the better option.

Why you need a queue

A queued job runs outside the visitor's request. Sending a confirmation email, generating a PDF invoice or calling a courier's API can take a few seconds; if you do it in the controller, the visitor waits for it. With a queue, the app saves the job and responds at once, and a separate process, the worker, runs it.

With QUEUE_CONNECTION=sync, jobs run immediately in the same request. It works, but you lose the whole benefit, and a failing job shows an error to the visitor.

Database or Redis

database redis
What you need just the app's database a Redis server
Setup the jobs table, included in the default migrations since Laravel 11 QUEUE_CONNECTION=redis and connection details
Speed enough for hundreds of jobs an hour faster, suited to high volumes
Horizon no yes, Horizon requires Redis

For a typical site, database is the simple choice. If your project lacks the migration for the jobs table, create it with php artisan make:queue-table and run php artisan migrate.

Option 1: a worker started by the scheduler

You need the scheduler's cron job, which runs php artisan schedule:run every minute. If you don't have it yet, the steps are in our article on the Laravel scheduler and cron. Then add to routes/console.php:

use Illuminate\Support\Facades\Schedule;

Schedule::command('queue:work --stop-when-empty --tries=3 --max-time=55')
    ->everyMinute()
    ->withoutOverlapping()
    ->runInBackground();

What each option does:

  • --stop-when-empty stops the worker when the queue is empty, so no process is left hanging;
  • --max-time=55 stops it after 55 seconds even if jobs remain; the next minute starts another;
  • --tries=3 attempts a job at most 3 times before marking it as failed;
  • ->withoutOverlapping() doesn't start a second worker while the first is still busy;
  • ->runInBackground() lets other tasks scheduled for the same minute start without waiting for the worker.

The limits of this option: jobs wait up to a minute, and a job that runs longer than your host allows for a process may be killed halfway. For long jobs, split the work into smaller jobs.

Option 2: a permanent, supervised worker

On a VPS, the worker runs permanently under Supervisor, which restarts it if it stops. The typical command is php artisan queue:work --sleep=3 --tries=3 --max-time=3600: --sleep=3 means the worker checks the queue again after 3 seconds when it finds nothing, and --max-time=3600 restarts it once an hour to free memory. Jobs start almost immediately. On hosting, this only works if your host offers supervised processes.

After every deploy: queue:restart

A worker loads your app's code once, when it starts. After a deploy, a worker started earlier keeps running the old code. Run php artisan queue:restart at the end of the deploy: workers finish their current job and exit, and Supervisor or cron starts them again with the new code. The command leaves its signal in the cache, so the cache has to work.

With the cron option, workers exit within a minute anyway, but queue:restart does no harm.

Failed jobs

A job that fails as many times as --tries allows ends up in the failed_jobs table. Useful commands:

  • php artisan queue:failed lists failed jobs, with each one's error;
  • php artisan queue:retry all puts them all back on the queue (or queue:retry followed by one job's ID);
  • php artisan queue:prune-failed --hours=168 deletes failed jobs older than a week; worth adding to the scheduler.

The full error also appears in storage/logs/laravel.log. If a job fails with no trace in the log, first check that storage is writable; the causes are the same as for the 500 error after a deploy.

Frequently asked questions

Can I run queue:work straight from cron, without the scheduler?

Yes, but without withoutOverlapping you risk starting a new worker every minute on top of the ones still running. Through the scheduler you get the lock for free.

Do I need queue:listen?

Not in production. queue:listen reloads the app for every job, so it's slower; it's only useful while developing, to pick up new code without a restart.

Laravel queues on AYT hosting

On AYT you don't need the cron trick. In the Laravel Toolkit, the Processes page has Add a process, where you pick Queue worker (php artisan queue:work), Horizon or your own command (the list also has Reverb and Octane), the number of copies and how long a job gets to finish when the process stops. Processes run supervised: they restart if they stop and after every deploy, so you don't run queue:restart by hand.

The Laravel Toolkit's Processes page with a queue worker and the Scheduler switch

You choose the driver under Settings, in the Queue field (database, sync or redis). On paid plans, Redis is switched on from the panel and available for Laravel, so you can use Horizon too. Run queue:failed and queue:retry all from the Commands page, without SSH. The rest of the Laravel tools are described under apps.

More articles

All articles