retry() { local t0 cur_date remaining local timeout="${1:?error: no timeout given}" local period="${2:?error: no period given}" declare -a cmd=("${@:3}") t0="$(date '+%s')" until "${cmd[@]}" do sleep "$period" cur_date="$(date '+%s')" remaining=$((t0 + timeout - cur_date)) [ "$remaining" -gt 0 ] || return 1 echo "$remaining seconds remain before teardown" done }