Skip to content

Instantly share code, notes, and snippets.

@obax
Last active March 10, 2024 15:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obax/cba31232c33a2494873d064c57540651 to your computer and use it in GitHub Desktop.
Save obax/cba31232c33a2494873d064c57540651 to your computer and use it in GitHub Desktop.
Rebooting the Raspberry Pi when it loses wireless connection
# Source: https://weworkweplay.com/play/rebooting-the-raspberry-pi-when-it-loses-wireless-connection-wifi/
# Save under /usr/local/bin/checkwifi.sh
# 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.
# 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 ping.
# 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
# sudo chmod 775 /usr/local/bin/checkwifi.sh
# Store under crontab (crontab -e)
# */5 * * * * /usr/bin/sudo -H /usr/local/bin/checkwifi.sh >> /dev/null 2>&
# 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
ping -c4 192.168.1.1 > /dev/null
if [ $? != 0 ]
then
sudo /sbin/shutdown -r now
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment