Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Does your Raspberry Pi's Wireless Interface freeze? This may solve it.

Does your Raspberry Pi's Wireless Interface freeze?

My Raspberry Pi 4 kept losing its wlan0 interface. I could usually reconnect via Ethernet but, from time to time, I noticed that the eth0 interface would also go walkabout.

I tried a lot of things but the one described here seems to have cured the problem. I have no idea why it works. It just does.

Step 0 - are you a Windows user?

The script shown in the next step should be created on your Raspberry Pi. Please do not make the mistake of selecting the text, copying it into a text editor on your Windows machine, saving the file, and then moving the file to your Raspberry Pi. Unless you take precautions, Windows will add its 0x0d 0x0a (CR+LF) line endings and those will stop the script from working properly on your Raspberry Pi.

Step 1 - create the script

Use your favourite Unix text editor to create this script:

#!/bin/bash

logger "isc-dhcp-fix launched"

Card()
{
ifconfig $1 | grep -Po '(?<=inet )[\d.]+' &> /dev/null
    if [ $? != 0 ]; then
        logger "isc-dhcp-fix resetting $1"
        sudo dhclient $1
    fi
}

while true; do
    Card eth0
    sleep 1
    Card wlan0
    sleep 1
done

Save it with the name isc-dhcp-fix.sh

If you don't have a favourite Unix text editor…

If you don't know how to use a text editor on the Raspberry Pi, follow these instructions:

  1. Connect to your Raspberry Pi via SSH.

  2. Select the script text above and copy it to the clipboard.

  3. Type the command:

    $ vi isc-dhcp-fix.sh
    
  4. Press the lower-case letter i once. That puts vi into insert mode.

  5. Paste the contents of the clipboard.

  6. Press the "esc" key once. That takes vi out of insert mode.

  7. Press the following keys:

    • : - puts vi into its command mode;
    • w - saves to isc-dhcp-fix.sh; and
    • q - tells vi to exit.
  8. Check your work like this:

    $ cat isc-dhcp-fix.sh
    

Step 2 - install the script

This assumes the isc-dhcp-fix.sh is in your working directory:

$ sudo cp isc-dhcp-fix.sh /usr/bin
$ sudo chmod +x /usr/bin/isc-dhcp-fix.sh
$ sudo chown root:root isc-dhcp-fix.sh

Step 3 - test the script

Type the following command:

$ isc-dhcp-fix.sh

The correct result is nothing. No error. No command prompt. Just silence. Wait a few seconds then press Control+C to terminate the command. Then type:

$ grep "isc-dhcp-fix" /var/log/syslog

You should expect to see at least one line containing the words "isc-dhcp-fix launched". If you do not see that, go back and check your work.

Step 4 - set the script running at boot time

Use your favourite text editor to open /etc/rc.local. It's a protected file so you will need sudo. The following example uses the vi text editor:

$ sudo vi /etc/rc.local

The last line of /etc/rc.local contains exit 0. Before the exit 0, insert this line:

/usr/bin/isc-dhcp-fix.sh &

Pay attention to the space followed by an ampersand ( &) on the end of the line. It means "run the script in the background".

Remember, the new line must be before the exit statement. When you have finished, the last part of /etc/rc.local should look something like this:

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/usr/bin/isc-dhcp-fix.sh &

exit 0

Step 5 - reboot

$ sudo reboot

Step 6 - check that it is working

If you did all the earlier steps correctly, rc.local will have spawned isc-dhcp-fix.sh as a background process during the start-up. You can see if everything is in place by:

$ grep "isc-dhcp-fix" /var/log/syslog

You should expect to see at least the "isc-dhcp-fix launched" from your earlier test, and another one from the reboot when isc-dhcp-fix.sh was launched by rc.local.

You will also see a log entry each time isc-dhcp-fix.sh senses that one of the interfaces has stopped working and has attempted a reset.

Hint:

  • I added that grep of the system log to my .profile. Whenever I login I get instant feedback if isc-dhcp-fix.sh has found it necessary to reset either interface. Keep in mind that the system log rolls over every 24 hours so you will only see today's events.

Acknowledgement

isc-dhcp-fix.sh is based on WillyWeiss/Avahi-daemon-withdraws-address-record.

@jaimefdeoliveira

This comment has been minimized.

Copy link

@jaimefdeoliveira jaimefdeoliveira commented Mar 4, 2021

Thanks !!!!

@877dev

This comment has been minimized.

Copy link

@877dev 877dev commented Apr 8, 2021

Instructions worked perfectly for me, I also added the grep isc-dhcp-fix" /var/log/syslog to my /home/pi/.profile file like suggested and it provides some useful info on login.
Cheers!

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