Skip to content

Instantly share code, notes, and snippets.

@carry0987
Last active April 2, 2025 07:26
Show Gist options
  • Save carry0987/372b9fefdd8041d0374f4e08fbf052b1 to your computer and use it in GitHub Desktop.
Save carry0987/372b9fefdd8041d0374f4e08fbf052b1 to your computer and use it in GitHub Desktop.
Raspberry Pi 3B+ Auto reconnect to wifi when lost connect

Auto reconnect to wifi when lost connection

Before you start, make sure ip command is available on your system. In modern Linux distributions, ip replaces older ifconfig command. If net-tools package (that includes ifconfig) is not installed and you prefer using it, you can install it via sudo apt-get install net-tools.

Create script file

Use touch /home/pi/wifi-reconnect.sh to create a shell script file, with the following content:

#!/bin/bash

SSID=$(/sbin/iwgetid --raw)

if [ -z "$SSID" ]; then
    echo "`date -Is` WiFi interface is down, trying to reconnect" >> /home/pi/wifi-log.txt
    if command -v /sbin/ip &> /dev/null; then
        /sbin/ip link set wlan0 down
        sleep 10
        /sbin/ip link set wlan0 up
    elif command -v sudo ifconfig &> /dev/null; then
        sudo ifconfig wlan0 down
        sleep 10
        sudo ifconfig wlan0 up
    else
        echo "`date -Is` Failed to reconnect: neither /sbin/ip nor ifconfig commands are available" >> /home/pi/wifi-log.txt
    fi
fi

echo 'WiFi check finished'

Or you can also use the following command to download the script.

sudo wget https://raw.github.com/carry0987/Raspberry-Pi-Repo/master/Auto-WiFi-Reconnect/wifi-reconnect.sh

Note:

This script uses /sbin/ip command for disabling and enabling the wlan interface. This is a more modern approach compared to using ifconfig and is the preferred way in newer Linux distributions.

Make new file executable

sudo chmod +x /home/pi/wifi-reconnect.sh

Install cron

sudo apt-get install cron

Edit crontab

Use sudo vim /etc/crontab to edit crontab

By putting the following content at the end of the file:

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

Test it by disconnecting from WiFi:

/sbin/ip link set wlan0 down

The script should reestablish the connection within 1 minute.

Check log file

After the RPi reestablishes the connection, reconnect to the RPi and check the log file:
cat /home/pi/wifi-log.txt

@cvaziri1
Copy link

cvaziri1 commented Apr 2, 2025

For anyone struggling with this on raspios (lite, bookworm, 2024), using the provided wifi utilities (NetworkManager via raspi-config or the official rpi image writer), I found that the default connection.autoconnect-retries property is set to "-1", which apparently means it will only try 4 times. Setting it to "0" should make it try to reconnect forever. I have configured this but have yet to have enough time pass to say whether this is the fix, but we'll see. It's pretty absurd to have to rely on a script for such an assumed default behavior. The nmcli connection modify command does survive reboots.

nmcli connection modify preconfigured connection.autoconnect-retries 0
nmcli con show preconfigured
connection.autoconnect-retries: 0 (forever)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment