我希望您能帮助我提供有关我的代码的想法,因为我不知道如果脚本在 if 条件下在 20 分钟内输入两次,我该如何验证。
rc=0
while [ $rc -ne 1 ]; do
do
ping 192.168.1.89
if [ "$?" -ne "0" ]; then
sleep 600 # this are 10 minutes
# here i need to validate if this script enters two times followed in 20 minutes
continue
else
continue
fi
done
欢迎任何帮助,谢谢
最佳答案
您似乎正在监控一个 IP 是否断开连接,并且您想要在相隔 10 分钟的两次 ping 失败时采取措施。为此,您只需使用一个计数器即可。
failures=0
rc=0
while [ $rc -ne 1 ]; do
do
ping 192.168.1.89
if [ $? -ne 0 ]; then
((failures++))
echo Ping failures: $failures
if (( failures < 2 )); then
sleep 600
else
echo Max failures hit
fi
else
failures=0
fi
done
您可能需要修改 ping 命令行以发送最大数量的 ping(例如,使用 -c 3
最多发送 3 个)。您可能希望将 ping 输出重定向到 /dev/null
。
关于linux - 如何在 bash 的 if 条件下验证时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928797/