Skip to content

Part 3. Reboot on Network Loss

Rachel edited this page Jan 5, 2017 · 2 revisions

To fix the network dropping issue, I decided to just implement a way for the Pi to detect a network connection and reboot if it's not there.

First we need to create a script to check the WiFi and then trigger shutdown:

sudo nano /usr/local/bin/checkwifi.sh

Place the following inside of the file, being sure to replace the IP address with the IP address of your router:

ping -c4 IP_ADDRESS > /dev/null

if [ $? != 0 ]
then
  sudo /sbin/shutdown -r now
fi

The ping checks for a connection. If it returns with a non-zero exit code, the script sends the shutdown command. Save and exit out of the script. Now make sure its permissions are in order:

sudo chmod 775 /usr/local/bin/checkwifi.sh

Just like our presence.py script, we are going to add this script to crontab:

sudo crontab -e

Place */5 * * * * /usr/bin/sudo -H /usr/local/bin/checkwifi.sh >> /dev/null 2>&1 underneath the line we added earlier. This will run our checkwifi script every 5 minutes.

Now exit crontab and reboot the Pi:

sudo reboot

Everything should be setup and working!

<< Part 3: Run the Script from Boot - Part 4: (Bonus) Text Alerts >>