我想在shell脚本中编写逻辑,如果由于某种原因而失败,它将根据“状态代码=失败”重试15秒后最多5次,再次运行5次。
我想在shell脚本中编写逻辑,如果由于某种原因而失败,它将根据“状态代码=失败”重试15秒后最多5次,再次运行5次。
Answers:
该脚本使用计数器n
将命令尝试次数限制为五次。如果命令成功$?
执行,将保持零,执行将从循环中止。
n=0
until [ $n -ge 5 ]
do
command && break # substitute your command here
n=$[$n+1]
sleep 15
done
if command; then break; fi
或更简明地是这样command && break
n
出现故障的情况下,它在退出之前不必要地再睡一会儿。
for i in 1 2 3 4 5; do command && break || sleep 15; done
用您的命令替换“命令”。假设“状态码=失败”表示任何非零返回码。
使用{..}
语法。适用于大多数shell,但不适用于BusyBox sh
:
for i in {1..5}; do command && break || sleep 15; done
使用seq
并传递失败命令的退出代码:
for i in $(seq 1 5); do command && s=0 && break || s=$? && sleep 15; done; (exit $s)
与上述相同,但sleep 15
在最终失败后跳过。由于最好只定义一次最大循环数,因此可以通过以下方式在循环开始时休眠来实现i > 1
:
for i in $(seq 1 5); do [ $i -gt 1 ] && sleep 15; command && s=0 && break || s=$?; done; (exit $s)
for i in 1 2 3 4 5
为for i in {1..5}
它,因为它更易于维护。
command
失败,它也会返回代码0 。
[[ i -eq 5]]
在睡前检查是否为OR条件,以避免这种情况。
function fail {
echo $1 >&2
exit 1
}
function retry {
local n=1
local max=5
local delay=15
while true; do
"$@" && break || {
if [[ $n -lt $max ]]; then
((n++))
echo "Command failed. Attempt $n/$max:"
sleep $delay;
else
fail "The command has failed after $n attempts."
fi
}
done
}
例:
retry ping invalidserver
产生以下输出:
ping: unknown host invalidserver
Command failed. Attempt 2/5:
ping: unknown host invalidserver
Command failed. Attempt 3/5:
ping: unknown host invalidserver
Command failed. Attempt 4/5:
ping: unknown host invalidserver
Command failed. Attempt 5/5:
ping: unknown host invalidserver
The command 'ping invalidserver' failed after 5 attempts
有关使用复杂命令的真实示例,请参见此脚本。
这是重试功能
function retry()
{
local n=0
local try=$1
local cmd="${@: 2}"
[[ $# -le 1 ]] && {
echo "Usage $0 <retry_number> <Command>"; }
until [[ $n -ge $try ]]
do
$cmd && break || {
echo "Command Fail.."
((n++))
echo "retry $n ::"
sleep 1;
}
done
}
retry $*
输出:
[test@Nagios ~]$ ./retry.sh 3 ping -c1 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.207 ms
--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.207/0.207/0.207/0.000 ms
[test@Nagios ~]$ ./retry.sh 3 ping -c1 localhostlasjflasd
ping: unknown host localhostlasjflasd
Command Fail..
retry 1 ::
ping: unknown host localhostlasjflasd
Command Fail..
retry 2 ::
ping: unknown host localhostlasjflasd
Command Fail..
retry 3 ::
bash retry.sh 3 ping -c1 localhost
GNU Parallel具有--retries
:
parallel --retries 5 --delay 15s ::: ./do_thing.sh
我使用该脚本重试给定命令,此脚本的好处是,如果所有重试均失败,它将保留退出代码。
#!/usr/bin/env bash
if [ $# -ne 3 ]; then
echo 'usage: retry <num retries> <wait retry secs> "<command>"'
exit 1
fi
retries=$1
wait_retry=$2
command=$3
for i in `seq 1 $retries`; do
echo "$command"
$command
ret_value=$?
[ $ret_value -eq 0 ] && break
echo "> failed with $ret_value, waiting to retry..."
sleep $wait_retry
done
exit $ret_value
可能会变得更简单
参见以下示例:
n=0
while :
do
nc -vzw1 localhost 3859
[[ $? = 0 ]] && break || ((n++))
(( n >= 5 )) && break
done
我正在尝试在本地主机上连接端口3389,它将重试直到5次失败,如果成功,则它将中断循环。
$?
如果为零表示命令成功运行,则为命令的存在状态;如果为零,则表示命令成功。
似乎有点复杂,也许有人做得比这更好。
$?
如果为零表示命令成功运行,则为命令的存在状态;如果为零,则表示命令失败
这是retry
函数式编程纯粹主义者的递归函数:
retry() {
cmd=$1
try=${2:-15} # 15 by default
sleep_time=${3:-3} # 3 seconds by default
# Show help if a command to retry is not specified.
[ -z "$1" ] && echo 'Usage: retry cmd [try=15 sleep_time=3]' && return 1
# The unsuccessful recursion termination condition (if no retries left)
[ $try -lt 1 ] && echo 'All retries failed.' && return 1
# The successful recursion termination condition (if the function succeeded)
$cmd && return 0
echo "Execution of '$cmd' failed."
# Inform that all is not lost if at least one more retry is available.
# $attempts include current try, so tries left is $attempts-1.
if [ $((try-1)) -gt 0 ]; then
echo "There are still $((try-1)) retrie(s) left."
echo "Waiting for $sleep_time seconds..." && sleep $sleep_time
fi
# Recurse
retry $cmd $((try-1)) $sleep_time
}
向其传递命令(或函数名称),还可以向其传递重试次数和重试之间的睡眠持续时间,例如:
retry some_command_or_fn 5 15 # 5 tries, sleep 15 seconds between each
break
命令是否成功,则它将中断循环