Script to auto restart openvpn service

hi everyone
I found this script and it should do what I want but I need to re-customized to do what I want
1- at the beginning give the router 3 minutes to boot and get internet connection
2- ping google every 5s
3- if the connection lost for 30s restart openvpn
can anyone help me with editing the code please?

#!/bin/sh
n=10
while sleep 50; do
        t=$(ping -c $n 8.8.8.8 | grep -o -E '\d+ packets r' | grep -o -E '\d+')
        if [ "$t" -eq 0 ]; then
                /etc/init.d/openvpn restart
        fi
done

Why wouldn’t you use openvpn built in options like keepalive 5 30?

because the problem is with my isp ... it keep disconnecting every hour or so
and every disconnect my orginal ip change and openvpn need to be restarted to work again

#!/bin/sh
IP=8.8.8.8  # Address to ping
PF=5        # Number of consecutive ping failures before restarting the service
COUNTERFILE=/tmp/counter.txt

sleep 180   #Initial delay

while true; do

if ! ping -c 1 -W 1 $IP >/dev/null; then
        if [ -f "$COUNTERFILE" ]; then
         counter=$(cat $COUNTERFILE)
        else
         counter="0"
        fi
        if [ "$counter" -ge "$PF" ]; then
         /etc/init.d/openvpn restart
         echo "0" > "$COUNTERFILE"
        else
         let "counter+=1"
         echo "$counter" > "$COUNTERFILE"
        fi
else
 echo "0" > "$COUNTERFILE"
fi
sleep 5
done
2 Likes

u know what ?
u r awesome :heart:
it worked

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.