skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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 IlluminateFoundationApplication;
    use IlluminateFoundationConfigurationExceptions;
    use IlluminateFoundationConfigurationMiddleware;
    
    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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search