Schedule in Laravel 11

We are facing an issue with Laravel scheduled tasks using the call method in Kernel.php not executing as expected in development mode on Windows. When running php artisan schedule:work, it only displays "No scheduled commands are ready to run," even though a simple task has been added.

Here’s the minimal example we’re using in Kernel.php:

protected function schedule(Schedule $schedule) { $schedule->call(function () {Log::info("Hello world"); })->everyMinute(); }

We expected to see "Hello world" appear in the log file (storage/logs/laravel.log), but it doesn’t.

Steps we’ve already tried:

Despite these attempts, the scheduled task is not working. Does anyone have suggestions to resolve this?

Upvotes: 0

Views: 908

Answers (2)

Thank you @AmirHossein AshariNik for your detailed answer showing how to use bootstrap/app.php. However, I've actually already solved my issue by moving the scheduling code to routes/console.php, as this appears to be the new recommended location for schedule definitions in Laravel 11.

Upvotes: 0

AmirHossein AshariNik
AmirHossein AshariNik

Reputation: 26

In Laravel 11, you can use bootstrap/app.php file, as well. Use the withSchedule function. Here’s how you can modify this file to include a scheduled task:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })
    ->withSchedule(function (Schedule $schedule) {
        $schedule->call(function () {
            info('Hello world!');
        })->everyMinute();
    })->create();

Test the scheduler locally using the Artisan command:

php artisan schedule:work

Upvotes: 0

Related Questions