Skip to content

Instantly share code, notes, and snippets.

@nathants nathants/retry.sh
Last active Sep 23, 2017

Embed
What would you like to do?
#!/bin/bash
# define the retry fn
retry() {
e=0
if [[ $- =~ e ]]; then
e=1
fi
set +e
max_tries=10
sleep_seconds=3
for i in $(seq 1 $max_tries); do
("$@")
if [ $? = 0 ]; then
break
else
echo retrying: "$@" 1>&2
sleep $sleep_seconds 1>&2
fi
if [ $i = $max_tries ]; then
echo all retries failed 1>&2
exit 1
fi
done
if [ e = 1 ]; then
set -e
fi
}
# export it so its available in subprocesses
export -f retry
fn() {
set -eou pipefail
# false # to trigger retries
echo process $1
}
# export it so its available in subprocesses
export -f fn
inputs="
a
b
c
"
# run the fn in parallel over the inputs, retrying failures
echo "$inputs" | xargs -n1 -P$(nproc) -I{} bash -c "retry fn {}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.