{"id":"8f9b0eb9bdb27901","repo":"laravel/framework","slug":"scheduled-closures-can-not-be-run-in-the-backgroun","errorCode":null,"errorMessage":"Scheduled closures can not be run in the background.","messagePattern":"Scheduled closures can not be run in the background\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Console/Scheduling/CallbackEvent.php","lineNumber":104,"sourceCode":"     * Determine if the event should skip because another process is overlapping.\n     *\n     * @return bool\n     */\n    public function shouldSkipDueToOverlapping()\n    {\n        return $this->description && parent::shouldSkipDueToOverlapping();\n    }\n\n    /**\n     * Indicate that the callback should run in the background.\n     *\n     * @return void\n     *\n     * @throws \\RuntimeException\n     */\n    public function runInBackground()\n    {\n        throw new RuntimeException('Scheduled closures can not be run in the background.');\n    }\n\n    /**\n     * Run the callback.\n     *\n     * @param  \\Illuminate\\Contracts\\Container\\Container  $container\n     * @return int\n     */\n    protected function execute($container)\n    {\n        try {\n            $this->result = is_object($this->callback)\n                ? $container->call([$this->callback, '__invoke'], $this->parameters)\n                : $container->call($this->callback, $this->parameters);\n\n            return $this->result === false ? 1 : 0;\n        } catch (Throwable $e) {\n            $this->exception = $e;","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Console/Scheduling/CallbackEvent.php#L86-L122","documentation":"Thrown by CallbackEvent::runInBackground() (src/Illuminate/Console/Scheduling/CallbackEvent.php:104) unconditionally. Closures/anonymous callbacks cannot be serialized to a background process command string, so Laravel refuses to background them.","triggerScenarios":"Chaining ->runInBackground() on a Schedule::call(...) or Schedule::job(...) event (both produce CallbackEvent). Also when a scheduled closure inherits background behavior (e.g. inside a Schedule::group() that sets runInBackground) — the merge triggers the method on the closure event.","commonSituations":"Copy-pasting a command-style schedule (which supports background) onto a closure-based schedule, or applying runInBackground at the group level without realizing closures are included.","solutions":["Remove runInBackground() from closure-based events and let them run in the foreground.","Move the logic into a real Artisan command and schedule it with Schedule::command('name') (a regular Event supports background).","Dispatch the work to a queue job instead and schedule the job (still foreground at the schedule layer).","If set at group level, split closures out of the grouped schedule."],"exampleFix":"// before\n$schedule->call(fn () => heavyWork())\n    ->daily()\n    ->runInBackground();\n\n// after\n$schedule->call(fn () => heavyWork())->daily();\n// or convert to a command:\n$schedule->command('reports:heavy')->daily()->runInBackground();","handlingStrategy":"validation","validationCode":"if ($event instanceof \\Illuminate\\Console\\Scheduling\\CallbackEvent) {\n    // do NOT chain runInBackground(); run in foreground or convert to a command\n}","typeGuard":"function isClosureEvent($event): bool\n{\n    return $event instanceof \\Illuminate\\Console\\Scheduling\\CallbackEvent;\n}","tryCatchPattern":"try {\n    $event->runInBackground();\n} catch (\\RuntimeException $e) {\n    // keep foreground execution\n}","preventionTips":["Don't apply runInBackground() to closure/job events.","For background work, schedule a real Artisan command via Schedule::command().","Avoid setting runInBackground at the group() level when closures are inside."],"tags":["scheduling","console","background-process","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}