Search code examples
laravellaravel-5laravel-4laravel-8scheduled-tasks

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:

  • Verifying the syntax of the Kernel.php file.

  • Running the appropriate Artisan commands: php artisan schedule:run and php artisan schedule:work.

  • Checking the permissions on the log file.

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


Solution

  • 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