diff options
author | EuAndreh <eu@euandre.org> | 2024-08-26 09:47:25 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-08-26 09:47:25 -0300 |
commit | 0078d30d0c7b3ab0f61eb96928f96fe668a19b8b (patch) | |
tree | 5bb11f49a581fa647000eed5edde92a7024245dc /src | |
parent | Makefile: Use correct $(sources) in "install" target (diff) | |
download | eut-0078d30d0c7b3ab0f61eb96928f96fe668a19b8b.tar.gz eut-0078d30d0c7b3ab0f61eb96928f96fe668a19b8b.tar.xz |
src/untill: Copy in untill(1) as is
Diffstat (limited to 'src')
-rwxr-xr-x | src/untill | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/untill b/src/untill new file mode 100755 index 0000000..9290cfe --- /dev/null +++ b/src/untill @@ -0,0 +1,94 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + until [-m MAX] [-n SECONDS] COMMAND... + until -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -n SECONDS the amount of seconds to sleep between + attempts (default: 5) + -m MAX the maximum number of attempts (default: unlimited) + -h, --help show this message + + COMMAND the shell command to be executed + + + Runs COMMAND until it eventually succeeds, trying atmost MAX + times. Sleep SECONDS between attempts. + + + Examples: + + Try flaky build until it succeeds: + + $ until guix home build home.scm + + + Try atmost 10 timex to build until a new version is successfull, + waiting 30 seconds between attempts: + + $ until -m 10 -n 30 -- x git pull AND make + EOF +} + + +for flag in "$@"; do + case "$flag" in + (--) + break + ;; + (--help) + usage + help + exit + ;; + (*) + ;; + esac +done + +_SECONDS=5 +while getopts 'm:n:h' flag; do + case "$flag" in + (m) + MAX="$OPTARG" + ;; + (n) + _SECONDS="$OPTARG" + ;; + (h) + usage + help + exit + ;; + (*) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + +ATTEMPT=1 +while true; do + printf 'Attempt %s.\n' "$ATTEMPT" >&2 + ATTEMPT=$((ATTEMPT + 1)) + if "$@"; then + break + fi + if [ -n "${MAX:-}" ] && [ "$ATTEMPT" -gt "$MAX" ]; then + printf 'Maximum number of attempts reached: %s.\n' "$MAX" >&2 + exit 1 + fi + sleep "$_SECONDS" +done |