commit 2a9b541023a86dd1e3bca8b37b7ae4c54a1c277c parent 729891197a86d763cfcd3689c0078c28430cb4a2 Author: Jacob R. Edwards <jacob@jacobedwards.org> Date: Thu, 25 May 2023 08:29:05 -0700 Add a script to persistently retry failed commands It's purpose is for cron jobs which require an internet connection when on an unstable connection. Since there could obviously be unrecoverable errors in some cases, a maximum number of attempts can be given. For example, to try at most five times with a 300 second delay between each attempt: persist -d 300 -m 5 connect host.org Diffstat:
| A | local/bin/bin/persist | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/local/bin/bin/persist b/local/bin/bin/persist @@ -0,0 +1,38 @@ +#!/bin/sh +# Copyright 2023 Jacob R. Edwards +# Persistently try a command until successful + +toomany() { + test $i -ge $max +} + +incr() { + i=$(expr $i + 1) +} + +while test $# -gt 0 +do + case "$1" in + (-d) delay="${2:?-d: no argument}"; shift ;; + (-m) max="${2:?-m: no argument}"; shift ;; + (-*) + echo 'Only -d allowed' 1>&2 + exit 1 ;; + (*) break; + esac + shift +done + +test -z "$delay" && + sleep() ; +test -z "$max" && { + incr() ; + tomany() ; +} + +i=1 +while ! "$@" && ! toomany +do + sleep $delay + incr +done