2015-04-29 44 views
0

超时参数,我可以使用超时为:组成的命令,在bash

timeout 5s sleep 6s 

但我怎么能传递给更多的超时复杂的命令?我试过以下,但所有给我的错误:几乎立即

timeout 5s sleep 6s && echo "You did not fit in timeout" 
timeout 5s 'sleep 6s && echo "You did not fit in timeout"' 
timeout 5s {sleep 6s && echo "You did not fit in timeout"} 
timeout 5s (sleep 6s && echo "You did not fit in timeout") 

回答

1

看一看这样的:

$ timeout 5 sleep 2 && echo OK || echo "You did not fit in timeout" 
OK 
$ timeout 1 sleep 2 && echo OK || echo "You did not fit in timeout" 
You did not fit in timeout 
+0

为什么你的&&被睡眠理解,而我的睡眠不是? –

+0

@WakanTanka:交互式shell解释'&&'和'||':如果'timeout'返回0,'&&'部分触发,否则执行'||'部分。如果该命令在超时过期之前终止,则超时的退出代码将为0;否则,请参阅联机帮助页'timeout(1)'。 –

0

张贴后,我想通了:

timeout 5s sh -c 'sleep 6s && echo "You did not fit in timeout"' 

有没有更好的解决办法?

+0

这个解决方案是错误的,因为它会打印'你不适合在timeout'只有当超时到期之前命令_was_终止。 –

+0

我知道:)我正在玩超时定时器来弄清楚它是如何工作的,这不是假设我只是想知道语法。 –

+0

现在我刚刚尝试过我的第一个例子'timeout 5s sleep 6s && echo“你不适合超时”,它工作。我认为这不是。这个问题是针对这个问题的。 –