【问题标题】:How to handle non-timeout exception of a script being run with timeout?如何处理超时运行的脚本的非超时异常?
【发布时间】:2020-02-26 00:31:02
【问题描述】:

我正在运行一个命令。如果|| 之后的部分失败,将被执行。超时也会执行。

$ timeout 5 script.sh || { echo "Would execute if timeout or script failure"; }

如何分别处理超时错误和脚本错误?

$ timeout 5 script.sh ??? { echo "Timeout is fine, but this is printed only if script.sh returns an error"; }

【问题讨论】:

  • 超时时,timeout命令的退出状态为124;您可以使用它来区分错误的类型,但这并不可靠,因为如果脚本本身返回 124,则无法分辨出区别

标签: bash timeout


【解决方案1】:

Oguz 是正确的。您需要检查 timeout 命令的退出代码。如果您可以查看 script.sh 以确保它不返回退出代码 124,则可以帮助降低 Oguz 确定的风险。

所以,我相信代码应该是这样的:

$ timeout 5 script.sh || [ $? -eq 124 ] || { echo "Timeout is fine, but this is printed only if script.sh returns an error"; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多