Simple Bash Function to Repeat a Command N times with Retries


Sometimes we want to repeat a command on Linux, and we can use the following BASH function to repeat a command for a number of times:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function repeat() {
        COUNT=$2
        if [[ -z "$COUNT" ]]; then
                COUNT=100
        fi
        for i in $(seq 1 $COUNT); do
                echo Counter=$i: CMD=$1
                if ! $1; then
                        echo "Error! $i"
                        return 1
                fi
        done
        return 0
}
function repeat() {
        COUNT=$2
        if [[ -z "$COUNT" ]]; then
                COUNT=100
        fi
        for i in $(seq 1 $COUNT); do
                echo Counter=$i: CMD=$1
                if ! $1; then
                        echo "Error! $i"
                        return 1
                fi
        done
        return 0
}

TO use, this is an example:

1
$ repeat "echo hello!" 10
$ repeat "echo hello!" 10

The second parameter specifies the number of times to run, and by default it is 100 times. We can use this command to repeat some tests, to find out if a test is flakey or not (stress testing). If at some point, the command/test failed, the loop will be terminated early.

Retry each command

We can also modify this function to let each run retry a few times, for example, the third parameter specifies the retry for each run, by default it is 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function repeat() {
        CMD=$1
        COUNT=$2
        RETRY=$3
        TOTAL=0
        if [[ -z "$COUNT" ]]; then
                COUNT=500
        fi
        if [[ -z "$RETRY" ]]; then
                RETRY=2
        fi
        echo "Running Total $COUNT times."
        for i in $(seq 1 $COUNT); do
                SUCC=0
                for j in $(seq 1 $RETRY); do
                        echo Counter=$i: CMD=$CMD, RETRY=$j
                        if ! $CMD; then
                                echo "Error! $CMD - RETRY=$j"
                                TOTAL=$((TOTAL+1))
                                sleep 3
                        else
                                SUCC=1
                                break
                        fi
                done
                if [[ "${SUCC}" == "0" ]]; then
                        echo "Failed at $COUNT runs, total retries = $TOTAL"
                        return 1
                fi
        done
        echo "OK at $COUNT runs, total retries = $TOTAL"
        return 0
}
function repeat() {
        CMD=$1
        COUNT=$2
        RETRY=$3
        TOTAL=0
        if [[ -z "$COUNT" ]]; then
                COUNT=500
        fi
        if [[ -z "$RETRY" ]]; then
                RETRY=2
        fi
        echo "Running Total $COUNT times."
        for i in $(seq 1 $COUNT); do
                SUCC=0
                for j in $(seq 1 $RETRY); do
                        echo Counter=$i: CMD=$CMD, RETRY=$j
                        if ! $CMD; then
                                echo "Error! $CMD - RETRY=$j"
                                TOTAL=$((TOTAL+1))
                                sleep 3
                        else
                                SUCC=1
                                break
                        fi
                done
                if [[ "${SUCC}" == "0" ]]; then
                        echo "Failed at $COUNT runs, total retries = $TOTAL"
                        return 1
                fi
        done
        echo "OK at $COUNT runs, total retries = $TOTAL"
        return 0
}

TLDR; this post presents a simple BASH function which is useful to repeat some command a few times, and we can also specify the retry for each run. This command may be useful in stress testing.

BASH Programming/Shell

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
371 words
Last Post: Teaching Kids Programming - N-Repeated Element in Size 2N Array (Math)
Next Post: Teaching Kids Programming - N-Repeated Element in Size 2N Array (Random Algorithm)

The Permanent URL is: Simple Bash Function to Repeat a Command N times with Retries

Leave a Reply