aboutsummaryrefslogtreecommitdiff
path: root/bin/try.sh
blob: 20ccbe5f7ce7fe511d1a226957edb731355bc522 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Attempt a certain number of times to perform a task, buffer stderr unless and
# until all command attempts fail
self=try

# Parse options
while getopts 's:n:' opt ; do
    case $opt in
        n)
            attn=$OPTARG
            ;;
        s)
            sleep=$OPTARG
            ;;
        \?)
            printf >&2 '%s: Unknown option\n' "$self"
            exit 2
            ;;
    esac
done
shift "$((OPTIND-1))"

# Check we have at least one argument left (the command to run)
if [ "$#" -eq 0 ] ; then
    printf >&2 '%s: Need a command to run\n' "$self"
    exit 2
fi

# Create a temporary directory with name in $td, and handle POSIX-ish traps to
# remove it when the script exits.
td=
cleanup() {
    [ -n "$td" ] && rm -fr -- "$td"
    if [ "$1" != EXIT ] ; then
        trap - "$1"
        kill "-$1" "$$"
    fi
}
for sig in EXIT HUP INT TERM ; do
    # shellcheck disable=SC2064
    trap "cleanup $sig" "$sig"
done
td=$(mktd "$self") || exit

# Open a filehandle to the error buffer, just to save on file operations
errbuff=$td/errbuff
exec 3>"$errbuff"

# Keep trying the command, writing error output to the buffer file, and exit
# if we succeed on any of them
attc=1
: "${attn:=3}" "${sleep:=0}"
while [ "$attc" -le "$attn" ] ; do

    # Try running the command; if it succeeds, we're done, and any previous
    # failures get their errors discarded
    if "$@" 2>&3 ; then
        exit

    # If the command failed, record the exit value
    else
        ex=$?
    fi

    # If this isn't the last run, have a sleep
    if [ "$attc" -lt "$attn" ] ; then
        sleep "${sleep:=0}"
    fi

    # Increment the attempt count
    attc=$((attc + 1))
done

# Attempts were exhausted, and all failed; print the error output from all of
# the failures and exit with the non-zero exit value of the most recent one
exec 3>&-
cat -- "$td"/errbuff >&2
exit "$ex"