You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exit code is always 0 for non-interactive runnerΒ #1339
We have jobs that rely on the exit code being non-zero on failures to e.g. be restarted or properly handled.
For that, we also have custom exit code mappers to make sure the exit code is set accordingly.
This is a valid issue. Thank you for raising it. However, the issue only happens when returning an ExitCode from the command. Throwing an exception from the command yields a non-zero exit as expected:
packageorg.springframework.shell.samples.noninteractive;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.context.annotation.Bean;
importorg.springframework.shell.core.command.*;
@SpringBootApplicationpublicclassSpringShellApplication {
publicstaticvoidmain(String[] args) {
System.setProperty("spring.shell.interactive.enabled", "false");
SpringApplication.run(SpringShellApplication.class, "fail");
// same output with the following://System.exit(SpringApplication.exit(SpringApplication.run(SpringShellApplication.class, "fail")));
}
@BeanpublicCommandfail() {
returnnewAbstractCommand("fail", "A command that always fails") {
@OverridepublicExitStatusdoExecute(CommandContextcommandContext) throwsException {
// throwing an exception will cause the app to exit with a non-zero exit code// throw new RuntimeException("Unable to execute command fail");// however, returning an exit status with a non-zero code will not cause the app to exit with that codereturnnewExitStatus(42, "Command execution failed");
}
};
}
}
Moreover, I see that it is not possible to return a value from ShellRunner#run (this design seems to have been always like that by looking at previous versions, and I guess it was designed in a similar fashion to Boot's ApplicationRunner#run / CommandLineRunner#run). Customizing the exit code is exception-based by design, through ExitCodeExceptionMapper:
packageorg.springframework.shell.samples.noninteractive;
importorg.springframework.boot.ExitCodeExceptionMapper;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.context.annotation.Bean;
importorg.springframework.shell.core.command.*;
@SpringBootApplicationpublicclassSpringShellApplication {
publicstaticvoidmain(String[] args) {
System.setProperty("spring.shell.interactive.enabled", "false");
SpringApplication.run(SpringShellApplication.class, "fail");
// same output with the following://System.exit(SpringApplication.exit(SpringApplication.run(SpringShellApplication.class, "fail")));
}
@BeanpublicExitCodeExceptionMapperexitCodeExceptionMapper() {
returnexception -> {
if (exceptioninstanceofCommandExecutionExceptione) {
returne.getExitCode(); // this is missing as of v4.0.1, but should be possible
}
return0;
};
}
@BeanpublicCommandfail() {
returnnewAbstractCommand("fail", "A command that always fails") {
@OverridepublicExitStatusdoExecute(CommandContextcommandContext) throwsException {
// with an exitCodeExceptionMapper, returning an exit status with a non-zero code should cause the app to exit with that codereturnnewExitStatus(42, "Command execution failed");
}
};
}
}
Therefore, the shell should indeed throw an exception in this case to be able to intercept it in any custom exception mapper.
Activity
fmbenhassine commentedon Apr 21, 2026
This is a valid issue. Thank you for raising it. However, the issue only happens when returning an
ExitCodefrom the command. Throwing an exception from the command yields a non-zero exit as expected:Moreover, I see that it is not possible to return a value from
ShellRunner#run(this design seems to have been always like that by looking at previous versions, and I guess it was designed in a similar fashion to Boot'sApplicationRunner#run/CommandLineRunner#run). Customizing the exit code is exception-based by design, throughExitCodeExceptionMapper:Therefore, the shell should indeed throw an exception in this case to be able to intercept it in any custom exception mapper.
Fix exit code in non-interactive shell