Skip to content

Instantly share code, notes, and snippets.

@carry0987
Last active April 17, 2024 04:50
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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

@carry0987
Copy link
Author

@Ekibunnel That's really weird, in my case it work just right, I'll test it on other raspberry pi next month.

@Ekibunnel
Copy link

@carry0987 could totally be my fault, it make quite some time since I did a proper fresh OS install on that pi.
I just wanted to share how I did solve it, in case someone one day as the same issue.

@carry0987
Copy link
Author

@Ekibunnel
I know, I just want to reproduce this kind of bug in other model of raspberry pi, which can point out the problem is a single case or not.
Since the code is really old (three years ago), so I must figure it out.

@JazzBrown1
Copy link

Hey works perfect. Thanks for sharing!

@carry0987
Copy link
Author

@jebeaudet
Copy link

jebeaudet commented Apr 21, 2022

Worked like a charm on OG pi Model B Revision 1.0 with Raspbian 9 Stretch and a usb wifi adapter, thanks!

@carry0987
Copy link
Author

@jebeaudet Glad it helps, thanks for your reply !

@madaddler
Copy link

madaddler commented Feb 27, 2024

I found that I had to replace this part of the shell script, otherwise, it appears to work well...

Replace...

sudo ifconfig wlan0 down
sleep 30
sudo ifconfig wlan0 up

with...

/sbin/ip link set wlan0 down
sleep 30
/sbin/ip link set wlan0 up

Pi 2 Model B Rev 1.1
Download Pi OS Lite
Release date: December 11th 2023
System: 32-bit
Kernel: Linux 6.1.0-rpi8-rpi-v7
Debian version: 12 (bookworm)

@carry0987
Copy link
Author

I found that I had to replace this part of the shell script, otherwise, it appears to work well...
TL;DR

Thans for your feedback!

The issue you encountered might be due to the net-tools package, which includes the ifconfig command, not being installed on your system. Your suggestion to replace it with /sbin/ip not only makes the script more aligned with current best practices (as ifconfig has been deprecated in favor of the ip command in most Linux distributions) but also ensures better compatibility with the latest versions of Pi OS Lite.

Considering that the ip command provides a more modern and universal interface for network management and is a standard part of many Linux systems, I will update the script to use /sbin/ip. Regarding the net-tools, I will add a note to the script's documentation to help others who might face the same problem.

Thanks again for your thorough feedback and suggestion. It's precisely contributions like yours that help improve the project for everyone :)

@LUKSAMA
Copy link

LUKSAMA commented Mar 21, 2024

Reeealy cool script bro... Thanks a lot

@carry0987
Copy link
Author

Reeealy cool script bro... Thanks a lot

Thanks :)

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