-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be0994f
commit 4fc964f
Showing
2 changed files
with
55 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env bash | ||
#shellcheck disable=SC1091 | ||
|
||
# Define the retry function | ||
waitandretry() { | ||
local waittime="$1" | ||
local retries="$2" | ||
local command="$3" | ||
local options="$-" # Get the current "set" options | ||
|
||
sleep "${waittime}" | ||
|
||
echo "Running command: ${command} (retries left: ${retries})" | ||
|
||
# Disable set -e | ||
if [[ $options == *e* ]]; then | ||
set +e | ||
fi | ||
|
||
# Run the command, and save the exit code | ||
$command | ||
local exit_code=$? | ||
|
||
# restore initial options | ||
if [[ $options == *e* ]]; then | ||
set -e | ||
fi | ||
|
||
# If the exit code is non-zero (i.e. command failed), and we have not | ||
# reached the maximum number of retries, run the command again | ||
if [[ $exit_code -ne 0 && $retries -gt 0 ]]; then | ||
waitandretry "$waittime" $((retries - 1)) "$command" | ||
else | ||
# Return the exit code from the command | ||
return $exit_code | ||
fi | ||
} |