【问题标题】:How to retrieve possible shell script exit codes when Executed shell script by Java DefaultExecutor & CommandLine?Java DefaultExecutor & CommandLine 执行 shell 脚本时如何检索可能的 shell 脚本退出代码?
【发布时间】:2021-12-09 12:21:21
【问题描述】:

我正在使用 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);
}

任何想法如何处理退出代码以执行特定的自定义操作?

【问题讨论】:

    标签: java shell command-line exit-code executor


    【解决方案1】:

    documentation for DefaultExecutor::execute() 说它会抛出

    ExecuteException - 子进程执行失败或子进程返回退出值指示失败

    ExecuteExceptionIOException 的子类,因此它会被您的代码捕获。如果您尝试(也)捕获正确的异常,则可以使用其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);
    }
    

    【讨论】:

    • 确实应该有一种方法来区分“运行程序失败”与“程序以失败状态退出”和“程序被信号杀死”。
    • 感谢并同意@Shawn。还应该有办法区分 成功退出代码失败退出代码(包括自定义失败退出和系统失败退出)寻找捕获成功状态代码的方法。 ExecuteException 中的 getExitValue() 肯定是完美的解决方法。
    【解决方案2】:

    经过更多探索,我能够找出正确的答案。

    对于 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);
    }
    

    完整的代码sn-p和解决方案

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2013-09-09
      • 2022-12-07
      相关资源
      最近更新 更多