Net watchdog for OpenWrt

Lately, my cable connection gets disconnected too often. To automate reconnection, I installed a simple watchdog script that pings outside address and if there is no response it restarts router network. It’s not most efficient, since only WAN needs to be restarted, but it does the job.

Here is a simple watchdog script.

#!/bin/sh
n=0
while [ $n -lt 10 ]
do
  if /bin/ping -c 1 8.8.8.8 >/dev/null
  then
    exit 0
  fi
  n=$((n+1))
done
/etc/init.d/network restart

First, ssh to router and create this script

ssh root@192.168.2.1
nano /root/watchdog.sh
chmod +x /root/watchdog.sh

Save it to /root/watchdog.sh, and give executable permissions
chmod +x /root/watchdog.sh
Add it to cron in order to execute it every minute:

* * * * * /root/watchdog.sh

You can either add this line using luci web interface at
http://192.168.1.1/cgi-bin/luci/admin/system/crontab/
or just edit /etc/crontabs/root file.

Make sure cron is enabled!