在bash中将命令作为超时参数

时间:2015-04-29 06:22:15

标签: 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")

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

几乎在发布后我立即想出来:

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

有没有更好的解决方案?