【问题标题】:Bash - Wait with timeout [duplicate]Bash - 等待超时[重复]
【发布时间】:2014-08-11 12:36:23
【问题描述】:

我对 bash 编程有疑问。我有这个代码:

#!/bin/bash
tmpfile="temp.txt"
./child.sh &
sleep 4s

但我想获取child.sh 的退出代码。我知道使用 costructor wait 是可能的。有没有wait+timeout的构造函数?

谢谢

【问题讨论】:

  • 你怎么知道child.sh 在 4 秒后完成?如果保证它是完整的,那么只需在脚本的前台运行它。如果它可能仍在运行,那么您需要检查这种情况。你可能想看看这个问题:stackoverflow.com/questions/1570262/….
  • 如果孩子没有做完,4秒后你想做什么?你想给它一个信号,还是只是意识到它不完整的事实?
  • 我在这里写了一个类似问题的答案:stackoverflow.com/a/41108021/771740

标签: linux bash timeout sleep wait


【解决方案1】:

你可以使用 wait 来等待你的 forked child 完成

#!/bin/bash
tmpfile="temp.txt"
./child.sh &
# next line wait for just previously forked process
wait $!
# next line exit with the status of previously returned command
exit $?

我想你想让孩子超时, 您需要的选项取决于您拥有的超时版本:

#!/bin/bash
tmpfile="temp.txt"
timeout 4 ./child.sh &
wait $!
exit $?

【讨论】:

  • 但是命令退出 $?引用命令超时。我希望它引用 ./child.sh
【解决方案2】:
cat file | xargs ./prova.sh; echo $?

这个命令返回xargs的退出码。我能得到prova.sh的退出码吗?

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多