Home‎ > ‎Server config‎ > ‎

Cronjob to restore and maintain Raspberry Pi wifi connection

The RPi wifi connection often does not come up after power failures because the routers are not available yet at boot time.

This script, which runs as a cronjob, checks for this and maintains the wifi connection at all times.

crontab:
*/5 * * * * /root/check_wifi.sh

Script:

#!/bin/bash
##################################################################
# JWS - 2019
# Script checks to see if WiFi has a network IP and if not
# restart WiFi
#
# Instructions:
#
# o Install where you want to run it from like /root
# o chmod 0755 /root/check_wifi.sh
# o Add to crontab
#
# Run Every 5 mins - Seems like ever min is overkill unless
# this is a very common problem.  If once a min change */5 to *
# once every 2 mins */5 to */2 ...
#
# */5 * * * * /root/check_wifi.sh

export PATH=$PATH:/sbin

# Which Interface do you want to check/fix
wlan='wlan0'

#echo
#echo "Starting WiFi check for $wlan"
#echo
#echo "Performing Network check for $wlan"

if !(ifconfig $wlan | grep -q "inet addr:")
then
    echo "Wifi is down - restoring..." >> /var/log/messages
    ifdown --force $wlan
    sleep 5
    ifup --force $wlan
    sleep 10
    mount -a
fi

exit 0