1

I am executing shell script using DefaultExecutor & CommandLine from Java Program.

For successful execution receiving 0 exit value

but in case of failure or any other exit code (like 127,128,255 etc.) from shell script, not receiving respective exit code instead getting IOException.

int iExitValue = 1;
CommandLine cmd = CommandLine.parse("sh /Users/DpakG/scripts/do_Database_Operations.sh");
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
    iExitValue = oDefaultExecutor.execute(cmd);
    log.info("Script Exit Code: " + iExitValue);
} catch (IOException e) {
    log.error("IOException occurred: ", e);
}

Any Idea How to handle exit code to perform specific custom action?

0

2 Answers 2

1

The documentation for DefaultExecutor::execute() says it throws

ExecuteException - execution of subprocess failed or the subprocess returned a exit value indicating a failure

An ExecuteException is a subclass of IOException, hence it getting caught by your code. If you instead try to (also) catch the correct exception, you can use its getExitValue() method to get the exit status.

int iExitValue = 1;
CommandLine cmd = CommandLine.parse("sh /Users/DpakG/scripts/do_Database_Operations.sh");
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
    iExitValue = oDefaultExecutor.execute(cmd);
    log.info("Script succeeded with exit code " + iExitValue); // Always successful (0)
} catch (ExecuteException e) {
    log.info("Script failed with exit code " + e.getExitValue());
} catch (IOException e) {
    log.error("IOException occurred: ", e);
}
Sign up to request clarification or add additional context in comments.

2 Comments

There really should be a way to differentiate "running the program failed" versus "the program exited with a failure status" and "the program was killed by a signal".
Thanks & Agree with you @Shawn. Also there should be way to differentiate between Success exit codes & Failure exit codes (including custom failure exit & system failure exit) Looking for Way to capture success status code. Also getExitValue() from ExecuteException is perfect workaround for sure.
1

After exploring more, I am able to figure out right answer.

For Success exit codes, use setExitValues() with int array of success codes instead of 'setExitValue()' with single exit code as integer value.

int[] codes = {0,127,128};
oDefaultExecutor.setExitValues(codes);

Rest of the Failure exit codes would be captured within ExecuteException block

catch (ExecuteException exe) {
    iExitValue = exe.getExitValue();
    log.info("Script failed with exit code: " + iExitValue);
}

Complete code snippet with solution

int iExitValue = 1;
CommandLine cmd = CommandLine.parse("sh /Users/DpakG/scripts/do_Database_Operations.sh");
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
int[] successCodes = {0,127,128};
oDefaultExecutor.setExitValues(successCodes);
try {
    iExitValue = oDefaultExecutor.execute(cmd);
    log.info("Script succeeded with exit code " + iExitValue); // Either 0, 127 or 128 
} catch (ExecuteException exe) {
    iExitValue = exe.getExitValue();
    log.info("Script failed with exit code: " + iExitValue);
} catch (IOException ie) {
    log.error("IOException occurred: ", ie);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.