如何在Bash中使一组命令超时 [英] How to timeout a group of commands in Bash
问题描述
我对Linux命令超时"不满意:它只是在给定的几秒钟后停止了长时间运行的命令.但是我不仅要使一个命令超时,还要使一组命令超时.我可以通过两种方式将命令分组()和{;},但以下任何一项均无效:
I am fiddling with Linux command "timeout": it simply stops a long running command after a given seconds. But I would like to timeout not only a command, but a group of commands. I can group command in two way ( ) and { ;} however none of the following works:
timeout 1 { sleep 2; echo something; }
timeout 1 ( sleep 2; echo something )
如何进行分组?
推荐答案
timeout
不是shell实用程序,它不执行shell样式的处理.必须给它一个命令才能执行.但是,该命令可以具有任意数量的参数.幸运的是,您可以提供的命令之一是bash
:
timeout
is not a shell utility and it does not do shell-style processing. It must be given one single command to execute. That command, though, can have any number of arguments. Fortunately, one of the commands that you can give it is bash
:
timeout 1 bash -c '{ sleep 2; echo something; }'
当然,以这种形式,大括号现在是多余的:
Of course, in this form, the braces are now superfluous:
timeout 1 bash -c 'sleep 2; echo something'
在这里,bash
是timeout
执行的命令. -c
和sleep 2; echo something
是该命令的参数.
Here, bash
is the command that timeout
executes. -c
and sleep 2; echo something
are argument to that command.
这篇关于如何在Bash中使一组命令超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!