【发布时间】:2017-11-24 13:03:03
【问题描述】:
我想在 bash 脚本中执行一个大约需要 1 分钟才能完成的命令。但是,有时这个命令会挂起,所以我想在循环中使用 /usr/bin/timeout 直到它起作用。
如果我使用timeout 300 mycommand myarg1,它可以工作,但如果我在下面这个循环中的 bash 中使用它,它不会打印任何东西(甚至不是我的命令打印的典型输出)并且它会挂起!:
until timeout 300 mycommand myarg
do
echo "The command timed out, trying again..."
done
我的 bash 版本:
$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
我的超时版本:
$ timeout --version
timeout (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
(标准 Ubuntu16.04)
【问题讨论】:
-
独立调用 (
timeout 300 mycommand myarg1) 和循环版本 (until timeout 300 mycommand myarg) 中的参数值不匹配。这只是一个错字还是您确实已经尝试过具有不同论点的两种场景? -
当我尝试使用只是一个 bash 脚本的命令时,它会休眠 2 秒并回显一些文本,它工作正常:
until timeout 1 cmd.sh; do ... ; done无限重复,...timeout 3...正常完成。你也应该试试这个。如果它在您的环境中有效,那么问题很可能是您的命令处理SIGTERM和终端的方式。要尝试的一件事是-k超时选项。
标签: bash loops while-loop timeout