russellxu's avatar

Run command in schedule, command run successfully but echo Failed?

I have a command, I run it directly in command line, it works file and have success info printed out. Then I put this command to schedule running every minute. When I run php artisan schedule:run , it marked this command as 'FAILED' instead of 'DONE'. But the code in this command are successfully runned. So,What might cause this problem?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

This could be caused by the fact that the command is not returning a successful exit code. By default, Laravel considers any non-zero exit code as a failure.

To fix this, you can modify your command to return a successful exit code explicitly. For example, at the end of your command, you can add:

return 0;

This will ensure that the command always returns a successful exit code, even if there were no errors.

Alternatively, you can modify the schedule to consider non-zero exit codes as successful by adding the following to your App\Console\Kernel.php file:

protected function scheduleTimezone()
{
    return 'UTC';
}

protected function commands()
{
    $this->load(__DIR__.'/Commands');

    $this->command('my-command')
         ->everyMinute()
         ->onSuccess(function () {
             return true;
         })
         ->onFailure(function () {
             return true;
         });
}

This will tell Laravel to consider any exit code as successful, regardless of whether it is zero or not.

1 like

Please or to participate in this conversation.