Bash: Retries Functions¶
for i in 1 2 3 4 5; do command && break || sleep 15; done
Replace "command" with your command. This is assuming that "status code=FAIL" means any non-zero return code.
Variations:
Using the {..} syntax. Works in most shells, but not BusyBox sh:
for i in {1..5}; do command && break || sleep 15; done
Using seq and passing along the exit code of the failed command:
for i in $(seq 1 5); do command && s=0 && break || s=$? && sleep 15; done; (exit $s)
Same as above, but skipping sleep 15 after the final fail. Since it's better to only define the maximum number of loops once, this is achieved by sleeping at the start of the loop if i > 1:
for i in $(seq 1 5); do [ $i -gt 1 ] && sleep 15; command && s=0 && break || s=$?; done; (exit $s)
Reference: https://unix.stackexchange.com/questions/82598/how-do-i-write-a-retry-logic-in-script-to-keep-retrying-to-run-it-upto-5-times