PromptHub
DevOps Intermediate 18 views

Laravel Schedule Task Failure

A scheduled task failed to execute or completed with errors.

Explanation

Laravel schedule task failure occurs when a command or closure defined in the schedule (app/Console/Kernel.php) fails during execution. This can happen because the task throws an exception, the command doesn't exist, the server cron job isn't running, or the task exceeds execution time limits. Scheduled tasks are only run when the server's cron daemon executes the Laravel scheduler, which is typically configured as * * * * * php artisan schedule:run. Failed tasks are logged and can be monitored.

Common Causes

  • Cron job not configured on server
  • Scheduled command doesn't exist
  • Task throws unhandled exception
  • Task exceeds time limit
  • Missing required environment

Solution

Verify the cron job is set up correctly on your server (run crontab -e to check). Test the scheduler manually with php artisan schedule:run. Check the Laravel log file for task-specific error messages. Use the onFailure() callback on scheduled tasks to capture failures. Ensure the task command or class exists and is spelled correctly. Use the schedule:work command for local development. Add php artisan schedule:run to your server's cron tab with the correct path to PHP and the artisan script.

Code Example

// app/Console/Kernel.php
protected function schedule(Schedule $schedule): void
{
    // This may fail if the command doesn't exist
    $schedule->command('reports:daily')
        ->dailyAt('08:00')
        ->onOneServer()
        ->onFailure(function () {
            Log::error('Daily report failed');
        });

    // Closure-based task
    $schedule->call(function () {
        // This could fail if models don't exist
        $stale = Cache::tags(['temp'])->get();
    })->everyFifteenMinutes();
}

// Server cron setup (crontab -e)
// * * * * * cd /path/to/project && php artisan schedule:run >> /dev/null 2>&1

// Test scheduler manually
php artisan schedule:run

Error Information

Language

php

Framework

laravel

Difficulty

Intermediate

Views

18

Related Errors