{"id":"53a63fcf167f40da","repo":"laravel/framework","slug":"scheduled-command-event-command-failed-with","errorCode":null,"errorMessage":"Scheduled command [{$event->command}] failed with exit code [{$event->exitCode}].","messagePattern":"Scheduled command \\[(.+?)\\] failed with exit code \\[(.+?)\\]\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"warning","filePath":"src/Illuminate/Console/Scheduling/ScheduleRunCommand.php","lineNumber":215,"sourceCode":"        );\n\n        $this->components->task($description, function () use ($event) {\n            $this->dispatcher->dispatch(new ScheduledTaskStarting($event));\n\n            $start = microtime(true);\n\n            try {\n                $event->run($this->laravel);\n\n                $this->dispatcher->dispatch(new ScheduledTaskFinished(\n                    $event,\n                    round(microtime(true) - $start, 2)\n                ));\n\n                $this->eventsRan = true;\n\n                if ($event->exitCode != 0 && ! $event->runInBackground) {\n                    throw new Exception(\"Scheduled command [{$event->command}] failed with exit code [{$event->exitCode}].\");\n                }\n            } catch (Throwable $e) {\n                $this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e));\n\n                $this->handler->report($e);\n            }\n\n            return $event->exitCode == 0;\n        });\n\n        if (! $event instanceof CallbackEvent) {\n            $this->components->bulletList([\n                $event->getSummaryForDisplay(),\n            ]);\n        }\n    }\n\n    /**","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php#L197-L233","documentation":"Thrown by ScheduleRunCommand::runEvent() (src/Illuminate/Console/Scheduling/ScheduleRunCommand.php:215) when a foreground scheduled command exits with a non-zero code. It is caught internally, reported via the exception handler, and surfaces as a failed task line in the schedule:run output; background events do not raise it.","triggerScenarios":"A scheduled Artisan command (Event, not CallbackEvent) returns non-zero exitCode (e.g. command throws, fails an assertion, or returns self::FAILURE). Triggered when schedule:run / schedule:work executes the due event.","commonSituations":"Scheduled maintenance/cleanup commands that error on bad input, missing dependencies, expired credentials, or DB connectivity. Often intermittent (network/API flakiness).","solutions":["Run the command manually with php artisan <name> to reproduce and read the actual exception.","Make the command robust: catch expected exceptions and return SUCCESS or a controlled FAILURE.","Add monitoring/logging inside the command; check storage/logs and the failed_jobs table.","If the failure is acceptable, run the event in the background to suppress this throw, or wrap with ->onFailure(...)/->emailOutputOnFailure(...)."],"exampleFix":"// before\n// command body throws -> exit code 1\n$schedule->command('reports:send')->daily();\n\n// after\n// in the command handle():\ntry { $this->send(); return self::SUCCESS; }\ncatch (\\Throwable $e) { $this->error($e->getMessage()); return self::FAILURE; }\n// and/or\n$schedule->command('reports:send')->daily()->onFailure(fn () => logger('reports failed'));","handlingStrategy":"try-catch","validationCode":"$exit = \\Illuminate\\Support\\Facades\\Artisan::call('reports:send');\nif ($exit !== 0) {\n    // don't schedule a known-failing command; fix it first\n}","typeGuard":"function commandSucceeds(string $name): bool\n{\n    return \\Illuminate\\Support\\Facades\\Artisan::call($name) === 0;\n}","tryCatchPattern":"// handled inside ScheduleRunCommand; in your command handle():\ntry {\n    $this->send();\n    return self::SUCCESS;\n} catch (\\Throwable $e) {\n    $this->error($e->getMessage());\n    return self::FAILURE;\n}","preventionTips":["Test scheduled commands locally before deploying.","Add ->onFailure() / ->emailOutputOnFailure() hooks for visibility.","Make commands idempotent and resilient to transient failures.","Monitor schedule:run output and storage/logs."],"tags":["scheduling","console","exit-code","task-failure","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}