aboutsummaryrefslogtreecommitdiff
path: root/contrib/respawn
blob: 4126cdbb3b5aeb9b3578bfebf549967526d7fbd4 (plain)
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
#!/bin/sh -e
# POSIX shell variant for ubase respawn

usage() {
    printf 'usage: %s [-d N] cmd [args...]\n' "${0##*/}"
    exit "${1:-0}"
}

die() { printf '%s\n' "$@" >&2; exit 1;}

case "$1" in
    -d) [ "$3" ] || usage 1
        [ "$2" -gt 0 ] || die "Not a number: $2"
        delay=$2; shift 2
        ;;
    --help|-h|'') usage ;;
    -*) usage 1
esac

while :; do
    "$@" || err=$?
    case $err in
        # Usually Ctrl+C interrupt.
        12[7-9]|130) exit 0
    esac
    [ "$delay" ] && sleep "$delay"
done