Automatically Reconnecting WiFi on a RaspberryPi (alexba.in/blog/2015/01/14/automatically-reconnecting-wifi-on-a-raspberrypi/) JAN 14TH, 2015 | COMMENTS In this post, I’m going to cover writing a short script that automatically reconnects a RaspberryPi to a WiFi network. The script will check to see if the Pi has network connectivity and, if it’s offline, will restart the wireless interface to bring it back online. We’ll use cron to schedule the execution of this script at a regular interval.
There are a few ways to determine if the RaspberryPi has network connectivity. For this script, we’ll be using ping.
Writing the script To get started, we’ll need to determine if the RaspberryPi is connected to the network. To do this, we’ll attempt to ping a server and see if we get a response. If the command succeeds (RaspberryPi receives a response from the server), we have network connectivity. If the command fails, we’ll turn wlan0 off and back on.
#!/bin/bash