Table of Contents

About

If you are not using a network manager then Debian-based distributions such as Raspbian for the Raspberry Pi will not attempt to bring up network interfaces after the interface went down. The following is a small script that can be ran via cron in order to periodically check whether the wireless interface has an IP address and, if not, bring the interface back up again.

Setup

Create the directory /etc/cron.minutely by issuing:

mkdir -p /etc/cron.minutely

and edit /etc/crontab to add an entry that will execute all the scripts from /etc/cron.minutely every minute:

* * * * * root cd / && run-parts --report /etc/cron.minutely

Copy the following script and place it under /etc/cron.minutely:

wifi-reconnect
#!/bin/bash
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3      ##
##  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  ##
##  rights of fair usage, the disclaimer and warranty conditions.        ##
###########################################################################
 
###########################################################################
##                            CONFIGURATION                              ##
###########################################################################
 
# An array of wireless interfaces.
IFACE_DEVICE=( wlan0 )
 
###########################################################################
##                              INTERNALS                                ##
###########################################################################
 
# Acquire a lock.
LOCK_FILE='/var/lock/wifi-reconnect'
if mkdir $LOCK_FILE 2>&1 >/dev/null; then
    trap '{ rm -rf $LOCK_FILE; }' KILL QUIT TERM EXIT INT HUP
else
    exit 0
fi
 
for i in ${!IFACE_DEVICE[*]}; do
    INTERFACE_ADDRESS=`ifconfig ${IFACE_DEVICE[$i]} |  grep 'inet' | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | head -n 1`
    if [ -z "$INTERFACE_ADDRESS" ]; then
        # The interface does not have an IP address.
        ifdown ${IFACE_DEVICE[$i]}
        if [ -z $WPA_RESTART ]; then
            killall -9 wpa_supplicant
            WPA_RESTART=1
        fi
        sleep 5
        ifup --force ${IFACE_DEVICE[$i]}
    fi
    # DEBUG
    #echo $INTERFACE_ADDRESS
done

and make it executable by issuing:

chmod +x /etc/cron.minutely/wifi-reconnect

The /etc/cron.minutely/wifi-reconnect may have to be edited to add additional wireless network interfaces to the IFACE_DEVICE array.

Finally, tell cron to reload its configuration file by sending a HUP signal:

kill -s HUP `pidof cron`

Testing

With the system in place, issue:

ifdown INTERFACE

where:

  • INTERFACE is the wireless interface to take down - such as wlan0.

After a minute or so, the wifi-reconnect script should run and bring the interface back up.


networking/wireless_reconnect.txt ยท Last modified: 2022/04/19 08:27 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.