mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

retry (436B)


      1 #!/bin/bash
      2 
      3 # retry command $NTIMES times until it succeeds
      4 # show stderr and stdout from the attempt that succeeded, or the last attempt
      5 
      6 NTIMES="$1"
      7 STATUS=0
      8 ERROUT=$(mktemp)
      9 STAOUT=$(mktemp)
     10 
     11 shift
     12 
     13 for a in `seq $NTIMES`
     14 do
     15   if [ $a -gt 1 ]
     16   then
     17     sleep 1
     18   fi
     19   "$@" 2>$ERROUT >$STAOUT
     20   STATUS="$?"
     21   if [ $STATUS -eq 0 ]
     22   then
     23     break
     24   fi
     25 done
     26 
     27 cat "$STAOUT"
     28 cat "$ERROUT" >&2
     29 rm -f "$ERROUT" "$STAOUT"
     30 exit "$STATUS"
     31