如何在 Java DefaultExecutor & CommandLine 执行 shell 脚本时检索可能的 shell 脚本退出代码?
How to retrieve possible shell script exit codes when Executed shell script by Java DefaultExecutor & CommandLine?
我正在使用 Java 程序中的 DefaultExecutor 和 CommandLine 执行 shell 脚本。
执行成功收到0退出值
但如果失败或任何其他退出代码(如 127,128,255 等) 来自 shell 脚本,则不会收到相应的退出代码,而是收到 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);
}
知道如何处理退出代码以执行特定的自定义操作吗?
documentation for DefaultExecutor::execute()
说它抛出
ExecuteException - execution of subprocess failed or the subprocess returned a exit value indicating a failure
ExecuteException 是 IOException
的子类,因此它会被您的代码捕获。如果您改为尝试(也)捕获正确的异常,则可以使用它的 getExitValue()
方法来获取退出状态。
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);
}
经过更多的探索,我能够找出正确的答案。
对于 Success 退出代码,使用 setExitValues() 和 int 成功代码数组而不是 'setExitValue()' 将单个退出代码作为整数值。
int[] codes = {0,127,128};
oDefaultExecutor.setExitValues(codes);
其余 失败 退出代码将在 ExecuteException 块
中捕获
catch (ExecuteException exe) {
iExitValue = exe.getExitValue();
log.info("Script failed with exit code: " + iExitValue);
}
带有解决方案的完整代码片段
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);
}
我正在使用 Java 程序中的 DefaultExecutor 和 CommandLine 执行 shell 脚本。
执行成功收到0退出值
但如果失败或任何其他退出代码(如 127,128,255 等) 来自 shell 脚本,则不会收到相应的退出代码,而是收到 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);
}
知道如何处理退出代码以执行特定的自定义操作吗?
documentation for DefaultExecutor::execute()
说它抛出
ExecuteException - execution of subprocess failed or the subprocess returned a exit value indicating a failure
ExecuteException 是 IOException
的子类,因此它会被您的代码捕获。如果您改为尝试(也)捕获正确的异常,则可以使用它的 getExitValue()
方法来获取退出状态。
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);
}
经过更多的探索,我能够找出正确的答案。
对于 Success 退出代码,使用 setExitValues() 和 int 成功代码数组而不是 'setExitValue()' 将单个退出代码作为整数值。
int[] codes = {0,127,128};
oDefaultExecutor.setExitValues(codes);
其余 失败 退出代码将在 ExecuteException 块
中捕获catch (ExecuteException exe) {
iExitValue = exe.getExitValue();
log.info("Script failed with exit code: " + iExitValue);
}
带有解决方案的完整代码片段
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);
}