Skip to content

[13.x] Fix scheduled foreground commands exiting with 0 on Laravel Cloud - #62707

Closed
asxxb wants to merge 1 commit into
laravel:13.xfrom
asxxb:fix/cloud-scheduled-command-exit-code
Closed

asxxb wants to merge 1 commit into
laravel:13.xfrom
asxxb:fix/cloud-scheduled-command-exit-code

Conversation

@asxxb

@asxxb asxxb commented Nov 30, 2026

Copy link
Copy Markdown

Fixes #62691

Problem

On Laravel Cloud, foreground scheduled commands always finish with exit code 0, even when the underlying command fails. Because $event->exitCode is always 0:

  • onFailure() and emailOutputOnFailure() callbacks never run.
  • ScheduledTaskFailed is never dispatched by schedule:run.

For example:

Schedule::command('config:show does.not.exist')
    ->everyMinute()
    ->emailOutputOnFailure('you@example.com');

The root cause is in CommandBuilder::buildForegroundCommand(). When laravel_cloud() is true, it wraps the command as:

command 2>&1 | tee file

In a shell pipeline, the exit code is the exit status of the last command (tee), which exits 0 as long as it can write its output regardless of whether command failed.

Solution

Update CommandBuilder::buildForegroundCommand() for laravel_cloud() to preserve the scheduled command's exit code using POSIX sh-compatible file descriptor redirection while still piping both stdout and stderr through tee:

exec 4>&1; exit_code=$({ { (command) 2>&1 3>&- 4>&-; echo $? >&3; } | tee file >&4; } 3>&1); exit ${exit_code:-1}
  • Uses pure POSIX sh syntax (compatible with dash, sh, bash, and zsh — no set -o pipefail / bash dependency, which is important because Symfony Process::fromShellCommandline() and ensureCorrectUser() invoke sh -c).
  • Preserves real-time streaming of stdout and stderr through tee to both $event->output (for sendOutputTo, appendOutputTo, emailOutputOnFailure, etc.) and stdout (which Event::execute() forwards to the Laravel Cloud log socket).
  • Closes the extra file descriptors (3>&- 4>&-) inside the command subshell so the command cannot write to or hold them open.
  • Works identically with user() (sudo -u ... -- sh -c ...) and appendOutputTo() (tee -a).
  • Leaves non-Cloud foreground commands and background commands unchanged.

Tests

  • Added unit tests in tests/Console/Scheduling/EventTest.php covering built command strings (default, appendOutputTo, user) and actual shell execution preserving non-zero exit codes, stdout/stderr, and file output/appending.
  • Added an integration test in tests/Integration/Console/Scheduling/ScheduleRunCommandTest.php verifying schedule:run on Laravel Cloud preserves the exit code, triggers onFailure() (and not onSuccess()), dispatches ScheduledTaskFailed, writes the output file, and forwards output to the Laravel Cloud log socket.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants