Keeping the Raspberry Pi Connected via Wi-fi

A few months ago, I set-up my Raspberry Pi with a wi-fi adapter. More recently, I’ve noticed that the wi-fi connection occasionally drops out, and the adapter never attempts to reconnect to the network. This is a problem if I’m trying to access the Pi via SSH.

At this stage, I’m still unsure as to why the wi-fi drops out. Initially, I thought there was an issue with my interfaces file however, adding “auto wlan0” to it in an attempt to force a reconnect failed. Trawling through the syslog files revealed that when the wi-fi dropped, a “link beat lost” message was recorded. I was fairly certain it wasn’t a power supply issue messing with the USB ports so my next idea was to create a cron job to check the status of the wi-fi adapter. This solution worked and I have outlined the steps I used below.

First of all, you need to create a new bash script in the following location (note, I’m using nano as my text editor):

cd ~
sudo nano ../../usr/local/bin/

The file has the following permission (the user can execute the file)

sudo chmod 0100 ../../usr/local/bin/

Next up, here’s the script I used to check the status of the wi-fi connection. What does it do? The Pi attempts to ping Google. If the Pi cannot successfully ping Google, then the script ensures the wi-fi interface is down before bringing it back up. Else, the wi-fi is connected and no action needs to be taken (yay!).

ping -c4 ${TESTIP} > /dev/null
if [ $? != 0 ]
    logger -t $0 "WiFi has gone down- run ifup"
    ifdown --force wlan0
    ifup wlan0
        logger -t $0 "WiFi is currently ok"

Now we need to create a new cron job to schedule when the wifi script runs. The new cron job goes here:

cd ~
nano ../../etc/crontab

How to add the new cron job to crontab:

*/5 * * * * root    /usr/local/bin/ >> /var/log/syslog 2>&1

The above line writes all the messages generated by the file to the syslog file (so “WiFi has gone down- run ifup” or “WiFi is currently ok”). The cron job is also scheduled to run every 5 minutes.

…and that should be it! Well, not quite. It’s probably a good idea to test if the script actually works.

Take the wi-fi interface down:

ifdown wlan0

Of course, this will break your SSH connection. Wait 5 minutes and the wi-fi interface should come back up, allowing you to SSH back into the Pi.

One last check. Have a look at the syslog file. You should see where the wi-fi disconnected and where it reconnected.

nano ../../var/log/syslog

Now you’re done!

Note: this is my first attempt at writing a bash script and a cron job so comments and criticism is welcome!  In the process of writing this blog post, I found Arne’s Blog to be particularly helpful.


  1. L

    This is great! Thanks!

  2. Garrett Sangalli

    All-in-one tutorial to staying connected to WiFi. +1

  3. Piet Jongen

    I get the message
    -bash: /var/log/syslog : Permission denied.
    Any idea how to solve this ?

  4. Lynsay

    You might need to run the command as root.

  5. Jools

    Hi Lynsay

    I found your post while searching for answers with the same problem with the same wifi adapter.

    I like your approach however I also found another post that identifies the actual issue, it’s with the Edimax EW-7811Un adapter’s power management. If you’re using the pi directly this won’t be an issue, it’s only when remotely connecting or if the pi is acting as a headless server of some kind.

    The power management of the Edimax will put the adapter in sleep mode and won’t respond to external connections. However you can override all the various PM options with a simple script. This may be more consistent a solution as the pi often reports wlan0 as up even thought the adapter is in sleep mode.

    All details on the Rpi support page here.

    Hope it helps.

  6. Kevin

    Here is my issue. Sometimes I loose power. When the power comes back up the Pi is quicker than the router in startup. I’ve tried using a similar script from Sam Hobbs site, but the program it seems to get stuck even after I added a 3min sleep period to try to wait for the router.
    I stink at code. That said, all I want is a simple program that checks eth0…if it’s up great. if not it’ll try to bring it up. if that fails than it’ll wait for a period of time and try again.
    Will your script do that?

  7. Lynsay

    I have a similar issue with my script. Basically, I start the Pi, I wait for 10 mins, then start running the script to ensure everything else on the system (network interfaces etc are up and running). I realise this isn’t the most efficient way to do it and I’m looking to find a way to trigger the script when everything has loaded.

    If your Pi is faster in booting than your router and you have the script running, it should be ok. The script will keep checking every 5 mins for a connection so when the router is ready, the Pi should connect (in theory!)

  8. Dan Donoghue

    Thank you so much for your simple and easy to follow documentation, I am new to linux and your instructions were great and when tested everything seems to work.

    I tried absolutely everything else I found on the net, I tried turning the power management off, I tried putting the power on the USB up, you name it I tried it and nothing ever worked for me.

    Your script works flawlessly, I don’t understand why this one works when nothing else did but thank you so much, you have saved me further frustration :).


    Dan Donoghue

  9. Adrian

    Hi, great article but what if we change interface file from wi-fi connection for a cable connection, and just plug off wi-fi dongle?

    /boot/interface file we set as below:

    iface eth0 inet static

    allow-hotplug wlan0
    iface wlan0 inet manual

    iface wlan0 inet static
    wpa-ssid “SSID here”
    wpa-psk “pass_here”

    it looks like we have both wlan0 and eth0 worked, but no, we will set router to ip from eth0 –, than only that configuration is connected. Ifconfig command show no wlan0 now (wifi usb dongle unplugged ofc) only eth0 and lo listed.

    And if the internet on eth0 is working it shouldn’t do anytning becouse ping is positive, yes? But if we unplug cable – no ping, this script will try restart wlan0 even if we haven’t listet wlan0 after “ifconfig” command? It will try script every 5 minutes or just without wlan0 it wouldnt even try to reconnect?

Leave a Reply

Your email address will not be published. Required fields are marked *

© L.Shepherd 2006-2021. All rights reserved.

Modified version of Hemingway Theme by Anders Noren