summaryrefslogtreecommitdiff
path: root/src/lib/process.source.sh
blob: 5596e034b48509459183aa43a656fa6d772399f6 (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
run_with_timeout() {
    local -r description="$1"; shift
    local -r seconds="$1"; shift
    local -i status=0

    if ! command -v timeout >/dev/null 2>&1; then
        printf 'ERROR: timeout command is required to run %s\n' \
            "$description" >&2
        return 127
    fi

    if timeout "$seconds" "$@"; then
        return
    else
        status=$?
    fi

    if (( status == 124 )); then
        printf 'ERROR: %s timed out after %s seconds\n' \
            "$description" "$seconds" >&2
    fi

    return "$status"
}