[Raspberry Pi] script for auto reconnecting WiFi connection

Published on Author admin

Create script file:

touch /home/volumio/wifi-reconnect.sh

with following content:

#!/bin/bash
 
SSID=$(/sbin/iwgetid --raw)

if [ -z "$SSID" ]
then
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/volumio/wifi-log.txt
    systemctl restart wireless
fi

echo "WiFi check finished"

Make new file executable:

chmod +x /home/volumio/wifi-reconnect.sh

Install cron:

sudo apt-get install cron

Edit crontab:

sudo vim /etc/crontab

by putting following content at end of file:

* * * * * root /home/volumio/wifi-reconnect.sh

Test it by disconnecting from WiFi, script should reestablish connection within 1 minute.

ifconfig wlan0 down

Reconnect RPi and check log file:

cat /home/volumio/wifi-log.txt