Everyone likes it when well made plans run smoothly. In my experience with technology, things could always run just a bit more smoothly… even the best laid plans can go horribly awry.
Sometimes, Pi goes offline… Maybe it’s overtaxed, maybe it’s sleepy. Happens, right? Sometimes, the wifi just drops dead for no easily identifiable reason.
Well, we can fix that.
Create a file a new script for checking connectivity with:
1 | sudo nano /usr/local/bin/wifiwatchdog.sh |
and enter the code:
1 2 3 4 5 6 7 8 9 10 11 | !#/bin/bash ping -c4 10.0.1.1 > /dev/null if [ $? != 0 ] then echo "No wifi, restarting wlan0" /sbin/ifdown 'wlan0' sleep 5 /sbin/ifup --force 'wlan0' else echo "All good!" fi |
Be sure to update the IP address you’re pinging to your local router. Save your work with CTRL X, then Y, finally ENTER.
Then set up a cron job to check it every 5 mins (or whatever interval suits your needs)
1 | sudo crontab -e |
and enter:
1 | */5 * * * * /usr/bin/sudo -H /usr/local/bin/wifiwatchdog.sh >> /dev/null 2>&1 |
Save by typing “:wq”