2012-09-07 146 views
26

如何重試bash命令,直到其狀態爲好或達到超時?重試超時Bash命令

我最好的拍攝(我在尋找簡單的東西):

NEXT_WAIT_TIME=0 
COMMAND_STATUS=1 
until [ $COMMAND_STATUS -eq 0 || $NEXT_WAIT_TIME -eq 4 ]; do 
    command 
    COMMAND_STATUS=$? 
    sleep $NEXT_WAIT_TIME 
    let NEXT_WAIT_TIME=NEXT_WAIT_TIME+1 
done 
+0

你不想把'$?'的權利放在測試語句中 - 我認爲這是一個錯字,你真的想要'$ COMMAND_STATUS'嗎? –

+0

@Grisha Levit:你說得對,我更新了這個問題 –

+0

這個表格有一個好處,那就是$ COMMAND_STATUS在執行後可用。 – AnneTheAgile

回答

37

您可以通過將command就在測試和做事情簡單化有點不同遞增位。否則腳本看起來很好:

NEXT_WAIT_TIME=0 
until command || [ $NEXT_WAIT_TIME -eq 4 ]; do 
    sleep $((NEXT_WAIT_TIME++)) 
done 
+4

至少在bash版本4.1.5中,您需要將睡眠行更改爲 sleep $((NEXT_WAIT_TIME ++)) – Nightscape

+0

謝謝,修復了答案。 –

+6

好的解決方案,唯一的問題是最後一個「命令」失敗後,你仍然需要睡4秒。不確定這是否可以避免,並保持代碼緊湊。 – David

9

把一些工具放在一起。

重試:https://github.com/kadwanev/retry

超時:http://manpages.courier-mta.org/htmlman1/timeout.1.html

然後看到最終通魔

retry timeout 3 ping google.com 

PING google.com (173.194.123.97): 56 data bytes 
64 bytes from 173.194.123.97: icmp_seq=0 ttl=55 time=13.982 ms 
64 bytes from 173.194.123.97: icmp_seq=1 ttl=55 time=44.857 ms 
64 bytes from 173.194.123.97: icmp_seq=2 ttl=55 time=64.187 ms 
Before retry #1: sleeping 0.3 seconds 
PING google.com (173.194.123.103): 56 data bytes 
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=56.549 ms 
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=60.220 ms 
64 bytes from 173.194.123.103: icmp_seq=2 ttl=55 time=8.872 ms 
Before retry #2: sleeping 0.6 seconds 
PING google.com (173.194.123.103): 56 data bytes 
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=25.819 ms 
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=16.382 ms 
64 bytes from 173.194.123.103: icmp_seq=2 ttl=55 time=3.224 ms 
Before retry #3: sleeping 1.2 seconds 
PING google.com (173.194.123.103): 56 data bytes 
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=58.438 ms 
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=94.828 ms 
64 bytes from 173.194.123.103: icmp_seq=2 ttl=55 time=61.075 ms 
Before retry #4: sleeping 2.4 seconds 
PING google.com (173.194.123.103): 56 data bytes 
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=43.361 ms 
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=32.171 ms 
... 

檢查退出狀態/失敗。

+0

管理單元:https://github.com/sky-shiny/retrycli – J0hnG4lt

9

重試機能的研究爲:

http://fahdshariff.blogspot.com/2014/02/retrying-commands-in-shell-scripts.html

#!/bin/bash 

# Retries a command on failure. 
# $1 - the max number of attempts 
# $2... - the command to run 
retry() { 
    local -r -i max_attempts="$1"; shift 
    local -r cmd="[email protected]" 
    local -i attempt_num=1 

    until $cmd 
    do 
     if ((attempt_num == max_attempts)) 
     then 
      echo "Attempt $attempt_num failed and there are no more attempts left!" 
      return 1 
     else 
      echo "Attempt $attempt_num failed! Trying again in $attempt_num seconds..." 
      sleep $((attempt_num++)) 
     fi 
    done 
} 

# example usage: 
retry 5 ls -ltr foo 

如果你想在你的腳本重試功能,你應該這樣做:

# example usage: 
foo() 
{ 
    #whatever you want do. 
} 

declare -fxr foo 
retry 3 timeout 60 bash -ce 'foo'