Help Needed, Periodically check of internet and reboot if needed

i'm trying to install and run the following script, but my knowledge in the matter isn't the best.
i made a file named pingRouterCheckReboot.sh on my pc with this content :


cat << "EOF" > /root/pingRouterCheckReboot.sh
#!/bin/sh

Prepare vars
DATE=$(date +%Y-%m-%d" "%H:%M:%S)
logFile="/root/pingRouterCheckReboot.log"

Ping and reboot if need
ping -c3 openwrt.org

if [ $? -eq 0 ]; then
echo "ok"
echo "${DATE} - ok" >> $logFile
else
echo "REBOOT"
echo "${DATE} - REBOOT" >> $logFile
reboot
fi

If the log file size is greater then 100KB (102400 bytes), then renew it.
logFileSize=$(wc -c "$logFile" | awk '{print $1}')
if [ $logFileSize -gt 102400 ]; then
echo "Size of $logFile = $logFileSize bytes."
echo "Renew the log file"

mv -f $logFile "${logFile}.bk"
fi
EOF

chmod +x /root/pingRouterCheckReboot.sh


then i copied pingRouterCheckReboot.sh file to the rooter root directory with winscp,
after that i pasted this :


Check every 15 minutes ping to google.com
If it is failed, then reboot the router
Note: To avoid infinite reboot loop, wait 70 seconds and touch a file in /etc
so clock will be set properly on reboot before cron starts.
0,15,30,45 * * * * sleep 70 && touch /etc/banner && /root/pingRouterCheckReboot.sh


to the openwrt router's Scheduled Tasks and pressed submit

original link
https://openwrt.org/docs/guide-user/base-system/cron

i have to idea if i've done anything right or wrong.

any help

thanks

@vgaetera -- Can you help? There's so much which needs fixing in the post above.

@tmomas -- despite the Vladislav's efforts to make the script presentable, do we really want something which will keep filling persistent memory in the wiki?

1 Like

I don't like that text in the wiki at all. I could understand logging the failures once they happen (just before the device is then rebooted), but logging each success every 15 minutes?

The wiki section is also strange, because it mixes the actual script with the wrapper script that creates the actual script. That has apparently confused also batsam. (it also talks about "bash script", although bash is just one non-standard alternative and the actual script has normal /bin/sh shebang)

2 Likes

I have no idea why the author of the script decided to utilize persistent logging.

In addition, there are multiple reasons for missing connectivity and reboot may not even help to restore it, and even if it helps, it may not be the best solution.

2 Likes

something closer to this... in a general structure sense?... very rough drapht... tough crowd :wink: shows logic...allows for hysteresis...

#!/bin/sh
#DATE=$(date +%Y-%m-%d" "%H:%M:%S)

tmpcheck="/tmp/.wancheck"

wancheck() {
#ping -c3 openwrt.org
ping -c3 openwrt.org &>/dev/null
if [ $? -eq 0 ]; then return 0; else return 1; fi
}

wandown() { :
	echo "wanisdown: $downcount"
}

wanup() { :
	echo "wanisup: $upcount"
}

doreboot() {
	rebootcount=$(expr $rebootcount + 1)
	echo "rebootcount=$rebootcount" > /etc/rebootcount
	touch /etc/banner
	sync
	#logger XYZ
	#reboot
}


doupcheck() {
	[ -f /tmp/.wancheck ] && . /tmp/.wancheck
	[ -f /etc/rebootcount ] && . /etc/rebootcount

	downcount=${downcount:-0}
	upcount=${upcount:-0}
	rebootcount=${rebootcount:-0}

	#echo "PRE    downcount: $downcount upcount: $upcount rebootcount: $rebootcount"
	if wancheck; then
		upcount=$(expr $upcount + 1)
	else
		downcount=$(expr $downcount + 1)
	fi
	updatecounts
	#echo "POST    downcount: $downcount upcount: $upcount rebootcount: $rebootcount"
}


updatecounts() {
	sed -i '/downcount/d' $tmpcheck &>/dev/null
	echo "downcount=$downcount" >> $tmpcheck
	sed -i '/upcount/d' $tmpcheck &>/dev/null
	echo "upcount=$upcount" >> $tmpcheck
}


[ ! -f /etc/rebootcount ] && echo "rebootcount=0" > /etc/rebootcount
doupcheck
#####.... if downcount -gt 123 && rebootcount -lt 3 >
#####.... if 3 hours passed reduce rebootcount or similar...
#TESTINGONLY doreboot; doupcheck; sleep 5
exit 0

Is there any merit in pinging the gateway rather than an arbitrary URL? Something like:

source /lib/functions/network.sh
network_find_wan NET_IF
network_get_gateway NET_GW "${NET_IF}"

tries=0
while [[ $tries -lt 5 ]]
do
        if /bin/ping -I pppoe-wan -c 1 "${NET_GW}" >/dev/null
        then
                echo "wan up"
                exit 0
        fi
        echo "wan down"
        tries=$((tries+1))
done
echo "wan failed 5 times - restarting"

ifup wan
2 Likes
wget -U "" -O - "https://openwrt.org/\
_export/code/docs/guide-user/network/\
wan/multiwan/mwan_netifd?codeblock=0" \
| sed -n -e "/^iface_check()\s{$/,/^}$/p" \
> /root/iface-check.sh
cat << "EOF" >> /root/iface-check.sh
. /lib/functions/network.sh
network_flush_cache
for IPV in 4 6
do
eval network_find_wan"${IPV%4}" NET_IF
if ! iface_check "${NET_IF}" 3 3
then logger -t netifd-watchdog \
"${NET_IF} is not responding - restarting"
ifup "${NET_IF}"
fi
done
EOF
2 Likes

watchcat is what you need

6 Likes

Be aware that if the default routing is set to OpenVPN tunnel, network_get_gateway may return empty/0.0.0.0 on a working connection. I believe it's also the case with the WG as well.

I think we should stop beating the dead horse, because:

  1. reboot doesn't necessarily help
  2. there's already a watchcat package as @LGA1150 mentioned

I suggest the wiki page is modified to mention watchcat instead of the custom script.

2 Likes

I tried and think that the elif condition for ping is the wrong way round:

    elif
        ping${PROTO} \
            -c "${PING_COUNT}" \
            -w "${PING_DEADLINE}" \
            -I "${NET_DEV}" \
            "${NET_GW}" &>/dev/null
    then
        logger -t netifd-watchdog \
            "${NET_IF} is not responding - restarting"
        ifup "${NET_IF}"
    fi

should be

    elif
        ! ping${PROTO} \
            -c "${PING_COUNT}" \
            -w "${PING_DEADLINE}" \
            -I "${NET_DEV}" \
            "${NET_GW}" &>/dev/null
    then
        logger -t netifd-watchdog \
            "${NET_IF} is not responding - restarting"
        ifup "${NET_IF}"
    fi

That is, a ! for a not negation in front of the ping. Only if we can not ping the gateways, then log and do an ifup.

This way it is working for me.

1 Like