File: retry_cmd.sh

package info (click to toggle)
nipype 1.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,260 kB
  • sloc: python: 156,463; javascript: 9,246; tcl: 608; sh: 485; makefile: 168
file content (36 lines) | stat: -rwxr-xr-x 648 bytes parent folder | download | duplicates (5)
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
34
35
36
#!/bin/sh
#
# retry_cmd.sh [-n NLOOPS] [-s SLEEP] CMD
#
# Retry command until success or pre-specified number of failures
#
# 2018 Chris Markiewicz
# Released into public domain

NLOOPS=3
TOSLEEP=5

while true; do
    case "$1" in
        -n ) NLOOPS="$2"; shift 2 ;;
        -s ) TOSLEEP="$2"; shift 2 ;;
        -- ) shift; break ;;
        * ) break ;;
    esac
done

# Normalize whitespace in command, preserving quotes
CMD=""
for ARG; do
    CMD="$CMD \"$ARG\"";
done

RET=0
for i in `seq $NLOOPS`; do
    sh -c "$CMD"
    RET="$?"
    if [ "$RET" -eq 0 ]; then break; fi
    if [ "$i" -ne "$NLOOPS" ]; then sleep $TOSLEEP; fi
done

exit $RET