gpt4 book ai didi

linux - 仅当 ping 中断时 Bash 才继续

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:55:57 28 4
gpt4 key购买 nike

我正在尝试创建一个仅在 ping 无响应时才会继续的脚本。

我遇到了 2 个主要问题。一个是它需要 2 个 CTL-C 命令来结束脚本,另一个问题是脚本永远不会结束,需要终止。

这是我的尝试;

rc=0
until [ $rc -gt 0 ]
do
ping 69.69.69.69 > /dev/null 2>&1
rc=$?
done

## Here will be the rest of code to be executed

我觉得上面这个非常接近,因为它需要 2 个 CTL-C 命令并继续。这是我在 SO 上找到的东西,但根本无法工作。

counter=0
while [ true ]
do
ping -c 1 69.69.69.69 > /dev/null 2>&1
if [ $? -ne 0 ] then
let "counter +=1"
else
let "counter = 0"
fi
if [ $counter -eq 10 ] then
echo "Here should be executed once pinging is down"
fi
done

非常感谢任何帮助,谢谢。

最佳答案

试试这个:

while ping -c 1 -W 20 "$host" >& /dev/null
do
echo "Host reachable"
sleep 10 # do not ping too often
done
echo "Host unreachable within 20 seconds"

关于linux - 仅当 ping 中断时 Bash 才继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650751/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com