gpt4 book ai didi

linux - 如何使用 ping 返回值来触发 bash 命令

转载 作者:太空狗 更新时间:2023-10-29 11:32:50 24 4
gpt4 key购买 nike

如果我的 openvpn 客户端无法每 60 秒 ping 一次目标 4.2.2.2,我会尝试让它重新启动,如果可以,则什么都不做。这是我所拥有的。我也想持续运行。我在 Alpine Linux 上运行它。非常感谢任何帮助。

#!/bin/sh
#This is a script to continuously do 5 pings to 4.2.2.2
#every 60 seconds to keep the tunnel up if pings fail,
#then it will restart the openvpn process. And record the
#time it failed.

PING=ping -w 5 4.2.2.2


exec &> /var/log/ping_SLA
while true
do
if
#ping returns a fail value

[ $PING -eq 0 ];
sleep 60s
then
#Execute commands

date > /var/log/ping_SLA_Fail
rc-service openvpn stop
killall -9 openvpn
rc-service openvpn start
sleep 30s
else
# colon is a null and is required
:

fi

done

最佳答案

我不熟悉 ping 的任何 -w 选项。

在 ping 命令中使用 -c 选项来确定要发送的 ICMP 回显请求的数量。

像这样的东西应该可以工作:

if ! ping -c 5 4.2.2.2 &> /dev/null; then
date >> /var/log/ping_SLA_Fail
rc-service openvpn stop
killall -9 openvpn
rc-service openvpn start
fi
sleep 60

关于linux - 如何使用 ping 返回值来触发 bash 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539815/

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