Skip to content

Setting up the Laravel scheduler

Laravel's scheduler runs your scheduled tasks, but only if something triggers it every minute. On your own server a single crontab line does that. On shared hosting without a crontab, a service such as Cronjob.de calls a protected address of your application. This page covers both ways, the limits of the second one, and how to notice when the scheduler stops.

How the scheduler works

You define your tasks in routes/console.php, each with its own schedule. Every time php artisan schedule:run starts, it checks which tasks are due right now, starts them and exits again. For that to work, something outside has to start it every minute.

routes/console.php

<?php

use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune')->daily();
Schedule::command('reports:send')->weekdays()->at('7:00');

php artisan schedule:list shows every task with its next run. On your own machine php artisan schedule:work starts the scheduler every minute until you stop it. Up to Laravel 10 the tasks live in app/Console/Kernel.php, in the schedule() method; that changes nothing about how it is triggered.

Option 1: One line in the crontab

With SSH access, add this line. Every minute it changes into the project directory and starts the scheduler:

* * * * * cd /var/www/project && php artisan schedule:run >> /dev/null 2>&1

Use the crontab of the user the web server runs as, usually www-data on Debian and Ubuntu: sudo crontab -u www-data -e. If the scheduler runs as root, newly created files in storage/logs belong to root and the website can no longer write to them. If several PHP versions are installed, write the full path of the version the website uses instead of php, for example /usr/bin/php8.3.

How to edit the crontab and check whether an entry ran is covered in Set up a cron job on Linux.

Option 2: Without a crontab, through an address

Many shared hosting plans have no crontab or only a coarse interval. Then your application gets an address that starts schedule:run, and Cronjob.de calls it every minute. A secret key makes sure nobody else can trigger the scheduler.

routes/web.php (the use lines go at the top of the file)

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;

Route::get('/cron/scheduler', function (Request $request) {
    $key = (string) config('services.cron.key');
    abort_if($key === '' || ! hash_equals($key, (string) $request->query('key')), 403);

    Artisan::call('schedule:run');

    return response(Artisan::output() . "OK\n")->header('Content-Type', 'text/plain');
});

Create the key in config/services.php and put the value into .env, for example CRON_KEY= followed by a long random string from php -r "echo bin2hex(random_bytes(24));". If your configuration is cached, run php artisan config:cache afterwards so the new value arrives. That is why the route reads the key through config(): with a cached configuration, env() returns nothing outside the configuration files.

config/services.php

'cron' => [
    'key' => env('CRON_KEY'),
],

Open the address with the key once in a browser. You see the output of schedule:run, such as “No scheduled commands are ready to run”, followed by OK. Then create a cron job at Cronjob.de with this address and the schedule * * * * *. From Plus we store the response in the log, so you see which tasks were started. In Unlimited you can set OK as text the response must contain; then a run that breaks off halfway also shows up as an error.

What is different from the crontab

Interval
The scheduler only starts tasks that are due in the minute of the call. So call the address every minute. On the free Cronjob.de plan that is only possible every five minutes. Then only tasks that fall on a full five-minute mark run, such as everyFiveMinutes(), hourly() or dailyAt('3:00'). A task for 7:12 would never be triggered. Every minute is available from Plus.
Run time
The call only ends once every due task has finished; if several run in the same minute, their times add up. Cronjob.de waits 45 seconds on the free plan, 60 on Plus and 90 on Unlimited, up to 5 minutes for single cron jobs there. Anything longer belongs in a queue, or in a process of its own with ->runInBackground().
Separate processes
Schedule::command() and Schedule::exec() start a separate process. Some shared hosts block that (the proc_open function). Then Schedule::call() is left, which runs inside the same request, for example Schedule::call(fn () => Artisan::call('model:prune'))->daily();
Sub-minute tasks
Tasks with everyTenSeconds() and similar methods keep schedule:run running until the end of the minute. They are not suited to being called through an address.
Maintenance mode
After php artisan down this address also answers with status 503. Cronjob.de shows that as an error, and the scheduler does not run until you are back up.

If a firewall or bot protection blocks the call, the page about our identifier lists the IP addresses and examples for an exception.

Noticing when the scheduler stops

If the crontab entry disappears, for example after a server move, nothing visible happens: the tasks simply stop running. Laravel can call an address after every task. If it points to a heartbeat at Cronjob.de, we alert you as soon as the check-in is missing.

routes/console.php

Schedule::command('backup:run')
    ->daily()
    ->pingOnSuccess('https://runcheck.de/a8k3m2x9q1')
    ->pingOnFailure('https://runcheck.de/a8k3m2x9q1/fail');

Schedule::call(fn () => null)
    ->everyFiveMinutes()
    ->thenPing('https://runcheck.de/b7n4p0r2s6');

The first task reports success to its address and a failure to the same address with /fail; we report that right away. The second does nothing except check in every five minutes. It shows whether the scheduler itself is still running. The addresses here are examples; you get your own when you create the heartbeat. One heartbeat is free.

Cron job monitoring with heartbeats: how it works →

Common questions

Why is my Laravel scheduler not running?
Usually the entry that starts schedule:run every minute is missing, or it runs in the wrong directory or with a different PHP version. Check with php artisan schedule:list whether Laravel knows your tasks, and run php artisan schedule:run once by hand in the project directory. In maintenance mode scheduled tasks do not run unless they carry ->evenInMaintenanceMode(). That applies to the crontab; through an address nothing runs at all, because it answers with status 503. More causes are in the troubleshooting guide.
Does the scheduler work without SSH and without a crontab?
Yes. A protected address in your application starts schedule:run, and a service such as Cronjob.de calls it every minute. For Laravel this is the same as the crontab entry, with two differences: the call has a time limit, and tasks that need a separate process fail at some hosts.
How do I stop a task from running twice?
With ->withoutOverlapping(). Laravel then sets a lock in the cache that expires on its own after 24 hours, or after the number of minutes you pass. If the application runs on several servers, ->onOneServer() makes sure only one of them starts the task. That requires the database, memcached, dynamodb or redis cache driver.

Have the scheduler called every minute

Five cron jobs are free forever at Cronjob.de, at five-minute intervals. Every minute is available from Plus for €17.88 a year. For the first 14 days you can try everything, including the one-minute interval and the stored response.

Start for free See pricing