The Raspberry Pi tends to drop network connection (especially wireless wifi) rather fast, which is a real pain when you're trying to do anything that has the RPi running constantly from a remote location (like our RaspEye does).
However, it's possible to detect wifi connection loss and perform upon it. It's easiest to just do a full system reboot.
Store this script in
Change the IP on the first line to the IP of your router, or some other device on your network that you can assume will be always online.
First step is to ping your IP.
On line three, the
$? represents the exit code of the previous command, in this case the
If it failed (not 0), the script assumes something's wrong with the wireless connection and just reboots.
Make sure the script has the correct permissions to run (thanks for the tip Jason Reibelt)
sudo chmod 775 /usr/local/bin/checkwifi.sh
SSH into the Raspberry Pi and open up the crontab editor by typing
Add the following line:
*/5 * * * * /usr/bin/sudo -H /usr/local/bin/checkwifi.sh >> /dev/null 2>&1
This runs the script we wrote every 5 minutes as sudo (so you have permission to do the shutdown command), writing its output to /dev/null so it won't clog your syslog.
Exit the crontab editor, reboot your Raspberry Pi (just to make sure) and from now on it'll reboot when it drops connection. Combine this with running your scripts on boot and you have a powerful, standalone system running.
Because many things could have gone wrong with a network loss and our application itself needs to adapt we prefer a full reboot. But after alot of people were shouting "that's just too much", you can also try to just restart the wireless connection: