2015-04-29 91 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「你不適合超時」,它工作。我認爲這不是。這個問題是針對這個問題的。 –