Guides · SaaS

Laravel queues and the task scheduler on a VPS: cron, Supervisor and what breaks.

Set up the Laravel scheduler with one cron line and keep queue workers alive with Supervisor. Covers retries, failed jobs and deployments.

Key takeaways

  • The scheduler needs exactly one cron entry that runs every minute: php artisan schedule:run.
  • Queue workers are long-running processes. Something must restart them if they stop, usually Supervisor.
  • After each deployment run php artisan queue:restart, because workers keep old code in memory.
  • Use a real queue driver (database or Redis) in production, not sync.

Two different things

People mix these up. The scheduler runs recurring tasks, such as sending expiry reminders every morning. A queue moves slow work, such as sending email or generating a PDF, out of the web request so the user is not kept waiting. They are set up differently.

The scheduler: one cron line

You define tasks in code. Laravel decides which are due. The server only needs to call Laravel every minute:

* * * * * cd /www/wwwroot/your-site && php artisan schedule:run >> /dev/null 2>&1

In aaPanel, add this under Cron as a shell script task, run every minute. Make sure the php path matches the PHP version your app uses, for example /www/server/php/83/bin/php.

An example schedule entry:

$schedule->command('reminders:send')->dailyAt('08:00')->withoutOverlapping();

withoutOverlapping() prevents two copies running at once if the first is slow.

Queues: choose a driver

Set QUEUE_CONNECTION in .env. sync runs jobs immediately inside the request, which defeats the purpose. database is easy to start with and needs the jobs table. redis is faster and suits heavier load.

Dispatch a job:

SendInvoiceEmail::dispatch($invoice);

Keeping workers alive with Supervisor

The worker is started with php artisan queue:work. If it crashes or the server reboots, nothing restarts it unless a supervisor does. A typical Supervisor program:

[program:laravel-worker]
command=php /www/wwwroot/your-site/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
user=www
numprocs=1
stdout_logfile=/www/wwwroot/your-site/storage/logs/worker.log

--max-time=3600 makes the worker exit and restart every hour, which clears memory growth.

Deployments and stale code

A worker loads your code once. After you deploy, it keeps running the old version. Run this at the end of every deployment:

php artisan queue:restart

Workers finish the current job, exit, and Supervisor starts fresh ones with the new code.

Retries and failed jobs

Set --tries or a $tries property on the job. Jobs that keep failing land in the failed_jobs table if you have run php artisan queue:failed-table and migrated. Inspect and retry:

php artisan queue:failed
php artisan queue:retry all

Make jobs idempotent: running one twice should not charge a customer twice or send duplicate emails.

Silent failures to check

  • Cron not running, so reminders never go out. Check the cron log.
  • Wrong PHP path in the cron line.
  • Worker running as root, writing files that the web user cannot read.
  • APP_ENV or config cached with old values after a change.
  • No monitoring. Look at failed_jobs regularly, or add an alert.

Frequently asked questions

Do I need Redis?

Not at first. The database driver is fine for modest volume. Move to Redis when throughput or latency demands it.

Can I skip Supervisor?

For anything important, no. A worker started by hand dies when your session ends or the server restarts.

How many workers should I run?

Start with one and add more if jobs queue up. More workers also mean more memory and more database connections.

Background jobs power reminders and billing in my SaaS builds.

See the credentialing SaaS