From 174dc54c5a31e9a90e5786fd51a8b0227ab441b0 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Sat, 12 Oct 2024 22:59:41 +0300 Subject: [PATCH] New retry script. Retry commands. --- Documents/bin/retry | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 Documents/bin/retry diff --git a/Documents/bin/retry b/Documents/bin/retry new file mode 100755 index 0000000..8cc714c --- /dev/null +++ b/Documents/bin/retry @@ -0,0 +1,40 @@ +#!/bin/sh +set -eu + +alias red="printf '\e[1;91m%s\e[0m\n'" +usage () { + echo "$(basename "$0"): RETRIES COMMAND [ARG1 [ARG2 [...]]]" + exit 1 +} + +if [ "$#" -lt 2 ] || [ "$1" -lt 1 ] +then + usage +fi + +retries="$1" +shift + +for _ in $(seq "$retries") +do + "$@" || status="$?" + if [ "${status:-0}" -eq 0 ] + then + break + elif [ "$_" -eq "$retries" ] + then + red "${1} failed too many times." >&2 + exit "$status" + else + case "$_" in + 1) duration=5;; + 2) duration=15;; + 3) duration=30;; + 4) duration=60;; + 5) duration=120;; + *) duration=300;; + esac + red "${1} failed, going to retry in ${duration} seconds." >&2 + sleep "$duration" + fi +done -- GitLab