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:
sudo nano /usr/local/bin/wifiwatchdog.sh
and enter the code:
ping -c4 10.0.1.1 > /dev/null
if [ $? != 0 ]
echo "No wifi, restarting wlan0"
/sbin/ifup --force 'wlan0'
echo "All good!"
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)
sudo crontab -e
*/5 * * * * /usr/bin/sudo -H /usr/local/bin/wifiwatchdog.sh >> /dev/null 2>&1
Save by typing “:wq”