gaboshlib/gaboshlib/g_retrycmd.bashfunc

29 lines
668 B
Plaintext
Raw Normal View History

2022-12-01 11:16:46 +01:00
#!/bin/bash
function g_retrycmd {
# If retry delay not set default to 5 seconds
[ -z "$g_tries_delay" ] && g_tries_delay=5
# If retrys not set default to 5 times
[ -z "$g_tries" ] && g_tries=5
2023-01-17 10:24:32 +01:00
#>&2 echo "$(g_date_print) Trying $@"
2022-12-06 13:58:54 +01:00
2022-12-01 11:16:46 +01:00
local g_try=0
until [ $g_try -eq $g_tries ]
do
((g_try=g_try+1))
2023-01-17 10:24:32 +01:00
#>&2 echo "$(g_date_print) Try $g_try of $g_tries"
echo "$@" >$g_tmp/g_retrycmd.cmd
bash $g_tmp/g_retrycmd.cmd && break
2022-12-01 11:16:46 +01:00
g_return=$?
2023-01-17 10:24:32 +01:00
if [ $g_try -eq $g_tries ]
then
[ -z "$g_return" ] || g_echo_error "${FUNCNAME}: $@ failed all $g_tries tries"
return $g_return
fi
2022-12-01 11:16:46 +01:00
sleep $g_tries_delay
done
2022-12-06 12:41:00 +01:00
return 0
2022-12-01 11:16:46 +01:00
}