November 10, 2010
Keep trying till you succeed with Bash

The following Bash snippet will repeat a command with the given delay until it succeeds.

while [ "$?" -gt "0" ] ; do
    sleep 120 # delay in seconds between retries
    # insert command to retry here
done

Unix processes return integers when they exit. These values can be read via $? in Bash. As a matter of convention, a process that completes without errors will return 0, so by checking for exit codes greater than zero we can detect errors and retry.

4:18pm  |   URL: http://tmblr.co/Zn_4by1Rb4ez
  
Filed under: bash cli 
  1. paksoy posted this