Mac Wi-Fi Dropping? Use a Simple Keepalive Bash Script to Maintain Wireless Connection

Aug 8, 2011 - 46 Comments

Mac Wi-Fi Keepalive Script

Many users who upgraded to Mac OS X Lion discovered that their Wi-Fi connections were dropping periodically for no apparent reason. We published a reasonably thorough walkthrough with tips on fixing OS X Lion’s wireless dropping issues and that’s a recommended starting point because most tips are easy and less complicated, but among those was a trick to maintain data transfer by pinging an IP address.

The keepalive ping technique seems to work, but it turns out you don’t need to ping an external IP, you can also just occasionally ping your local wifi access point. With this in mind, we’re going to create a simple keepalive script that will run from the command line and ping your router every 5 seconds, allowing the wifi connection to maintain itself and prevent a drop.

1) Get your WiFi Router IP Address

You’ll need to know your wireless access points IP address before proceeding, it’s usually something like 192.168.0.1 or 192.168.1.1.

You can get this information from System Preferences > Network > Advanced > TCP/IP and looking for the IP next to “Router”:

Get WiFi Router IP Address in Mac OS X

Make note of that IP and proceed with the following:

2) Create the Keepalive Bash Script

  • Launch the Terminal (located in /Applications/Utilities/)
  • Type the following command:
  • nano keepalive.sh

  • Paste in the following, be sure to replace the IP with your own routers:
  • #!/bin/bash
    ping -i 5 -n 192.168.1.1

  • Hit Control+O to Save the contents of keepalive.sh
  • Hit Control+X to exit from nano

3) Run the Wi-Fi Keepalive Bash Script

  • Back at the command line, we have to make the script executable, we do this with:
  • chmod +x keepalive.sh

  • Now to run the keepalive script, we type:
  • ./keepalive.sh &

That last command starts and runs the keepalive.sh script in the background. Your wireless connection should stay alive now and dropping should come to an end.

The idea of creating a simple bash script comes from Ahmet C. Toker, who left the trick in our comments and said that after he ran it “the problem melted away” and the wifi stopped dropping. It’s less of a fix and more of a simple workaround, but it does seem to maintain wifi connections, and it’s probably better than pinging an external IP address like yahoo.com for a number of reasons.

The remaining question is if there is a bug with how Mac OS X 10.7 handles wifi connections, or if some routers just don’t play well with OS X. I think it’s the latter, because I’ve only encountered the problem on select brands of routers and others are flawless, but for all routers I have found a solution that stopped the connection failures completely. It’s possible that a future update to OS X 10.7 will resolve the issue completely for everyone.

If you’re still having troubles you can review our multitude of past guides on troubleshooting wireless issues in Mac OS X:

Good luck!

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks, Troubleshooting

46 Comments

» Comments RSS Feed

  1. rod says:

    People should just do a clean install… that will avoid that problem.

  2. DJay says:

    Thanks for that Workaround.
    But just in case how can I revert back to the original state?
    Thanks for your reply.
    DJay

    • OSXDaily says:

      Hey DJay,

      Because you have created the script yourself, you can just kill the process at any time to stop it. If you don’t want it anymore, you can also just delete the file, which will be located in your home directory.

  3. gr33n3ggz says:

    I have it on my MBPro. Did a clean install. Drops all the time. The bash is nice but when you take it to work then home or anywhere else this can get tricky. this will work if you stay in one spot.

    Apple just needs to fix the issue. This version of os x reminds me of Windows ME or Vista.

  4. gr33n3ggz says:

    I have it on my MBPro. Did a clean install. Drops all the time. The bash is nice but when you take it to work then home or anywhere else this can get tricky. this will work if you stay in one spot.

    Apple just needs to fix the issue. This version of os x reminds me of Windows ME or Vista.

  5. Delblogo says:

    Applecare senior support was able to resolve this issue for me. They had me delete the dns addresses that were there System preferences>network> advanced. Click on the DNS tap. Delete whatever is there and input 8.8.8.8 and 8.8.4.4 and I haven’t had any dropping issues since.

    • Shaun says:

      8.8.8.8 and 8.8.4.4 are the Google Public DNS servers.

    • Carlton8429 says:

      BLESS YOU! 3 days 2010 MBA not recognising Netgear DGN3500 wireless password (same for over 2 years).

      Removing Bluetooth entries in Network Prefs didn’t work.
      Removing keychain entries didn’t work.
      Deleting preferred networks and unchecking ‘Remember networks…’ didn’t work.

      This did work, exactly as Delblogo:
      Adding 8.8.8.8 and 8.8.4.4 in DNS Servers under DNS tab (Mine was empty to begin with).

      Can cancel my appointment with an Apple Genius now!

  6. boing says:

    I only have problems with a DLINK router, so I am convinced it is how the Airport card interacts with some router firmware. This is probably a simple software fix that will come in OS X 10.7.1 update due out any day now.

  7. Matt says:

    does this work on previous OSX ?
    I have a DSL connection but using wifi via AirMacExpress with AirTunes keeps on dropping the connection multiple times a minute.
    Unfortunately, i work so it is very hard for me to contact Customer service which is available only during normal working hours here in this country.

  8. [...] issues with your WiFi after upgrading to Lion, then we have a solution for you. The folks over at  OSX Daily have posted this neat little Bash Script to maintain your wireless connection. This walkthrough [...]

  9. Kunyukus Marsialus says:

    I’m using (the simpler) Network Utility to do that :)

  10. Chris Richard says:

    I did a clean install of Lion on my MBPro, and I constantly have this issue.

    I’m doing the keepalive script, and it seems to work fine, but do I have to keep the terminal window open all the time to work? And, have to open & run the script every time I boot my laptop?

    • Will says:

      Yes, you will need to run the script on each reboot. You could schedule it to run automatically, but I would expect an update to Lion in the near future to iron out a few bugs, so hopefully that wouldn’t be necessary.

  11. Nash says:

    I agree with Kunyukus Marsialus. I think it does the same with Network Utility -> Ping (192.168.2.1) Unlimited. Why bother making a script if your one of those guy who are afraid of looking codes

  12. vaddieg says:

    Keychain cleanup (all wi-fi passwords) solved the problem.
    MBP 2006

  13. Flyingdutchman says:

    just make a crontab entry and you don’t have to bother anymore !!

    within terminal :
    crontab -e ( acts like VI-editor)

    # to keep WLAN active
    45 * * * * /sbin/ping -i 60 -c 59 yahoo.com > /dev/null

    Cheers René

    MBP 15

  14. Leonardo says:

    Where’s the need of a script ? Why just not opening a dedicate terminal window and start pinging from there ? Also, at home, instead of pinging external addresses, it works for me to ping my wi-fi router.

  15. Havi says:

    Got as far as:
    Hit Control+O to Save the contents of keepalive.sh
    Control+X just gives me a warning beep and does nothing.

  16. Havi says:

    Thanks Paul,

    You know your stuff. It worked!

  17. chriztoph3r says:

    Ive posted this to the other thred “http://osxdaily.com/2011/07/22/wifi-dropping-in-os-x-lion-fixes/#comment-241576″

    [SOLVED!!]
    Forget all the other tips and troubleshooting stuff.
    I have read a solution somewhere, forgot where but it solved my problem.

    Go to System Preferences > Network > WiFi Must be selected then click Advanced button.

    Under TCP/IP tab set “Configure IPv6″ to “Link-local only”
    Click OK button. Your Done!

    Try to sleep/shutdown your mac, when it wakes up try to surf. Your already connected.

    You don’t have to reset/turn off and on your router.

    Tested on:
    MBP 15″ unibody
    OS X Lion 10.7.1
    2.2 Ghz i7
    4GB RAM

    Hope this helps a lot!

    btw, my router is a belkin on firmware 2.01.08 (Nov 19 2009 10:25:35)

    Reply on my post and keep me updated whether this works for you or not. Thank you.

  18. Ikort says:

    Hi, I have created the keepalive bash script as described above and its running. But should I keep the terminal window open / minimized? If I try to close it, I receive the messagebox saying that if I close the window, the login, bash and ping command will be stopped.

  19. Liesl says:

    I tried this, but after typing in #!/bin/bash
    ping -i 5 -n (my router info) and pressing control and O, the system doesn’t allow me to press control X to exit.

    When I try to make the script executable and to run it, the system says “No such file or directory”.

    What am I missing? I have an iMac that came installed with Lion. Everything worked fine until an automatic update over the weekend. Now the connection drops if I am not actively surfing/skyping.

    please help

  20. Wayne Neale says:

    OK, this bash script installed and ran perfectly…but did NOT stop the (new, with clean OSX Lion Install) iMac from losing the network every time it sleeps.

    Getting desperate now – tried everything.

    Any ideas, or is this operating system just hopeless with wifi? Have another iMac with SL, iPads etc and everything else is rock solid on wifi. Spoken to Apple a number of times to no avail, yet surely they must be aware of this issue by now?

    Maybe take this to Tim Cook?

  21. pbproms says:

    Me the same, cannot get my MBPro (2009ish) to hold its wifi connection at all, and after trying every conceivable cure I can find. I’m running 10.7.2 and connected to O2’s box via a Time Capsule. I’m actually using the time capsule’s wifi, not the O2 box, that is just simply a modem.
    This problem is totally unacceptable, and me like a lot of other people I presume, wish I had never ever heard of Lion, and I’m certainly not moving to iCloud until I see some 100% positive feedback on that, – come on Apple, I know you’ve had some set back’s recently, but this should be an easy one to sort out for you guy’s???

    Incidently, once you have this keep alive script running, how do you stop and / or delete it. (Sorry if I’m being stupid here, but it doesn’t seem to be that obvious to me??) Cheers.

  22. Grant Wray says:

    Slightly different take on the same issue.
    I have two MacMinis on which I intend to run a single, auto-running on auto-login, App in a headless (no screen, no keyboard) mode.
    When these machines (10.7.2) startup, they connect to the wireless network fine, and will stay connected for hours (I turned sleep etc. off).
    If the wireless router freezes or power cycles or the WiFi is blocked in some way, then the MacMinis will NOT auto-join the network when it becomes available again. Even running a keep-alive script, the network must be rejoined manually by e.g. selecting it from the drop-down list.
    I consider it a fault in the routing protocol. Instead of “Network traffic – let’s find a route. Auto-join known network. Route packet.” it’s doing “Network traffic – let’s find a route. No known route. Drop packet.”

    I’ve done a quick and dirty Automator script that hits the network list every 30 seconds, but that is really a inelegant solution. Apple Tech just told me it’s “not a supported scenario”, but I reckon that for people with weak signals, that’ll be why they are getting network outage.

  23. zek says:

    Thanks for the idea… I’m trying it out now and hope it works.

    However, I do think it’s possible to consider that it’s a software issue.

    I have bootcamp and 2 routers. while connected on lion to either router, both drops for no apparent reason after awhile. Regardless if i’m playing an online game or simply not online. can tell because my dropbox stops syncing.

    However when i reboot into windows. No problems whatsoever.

    Hope this feedback helps whoever in developing a solution.

  24. Ray says:

    This is just a disgrace from Apple, all these people running around trying to help each other …come on APPLE sort it, even the latest fix is not working

  25. Frank says:

    So, how do you “keep it running”? Can you close Terminal and still have it running in the background or does Terminal have to stay open the whole time?

  26. Anney says:

    Unfortunately, it didn’t work for me.
    It showed ‘Request timeout for icmp_seq 16′ and just reconnected anyway.
    Can anyone tell me what that means?

  27. Daniel says:

    When i execute keepalive appear the following msg

    /sbin/ping: /sbin/ping: cannot execute binary file
    [1]+ Exit 126

    and it not work

    what can i do?

  28. robert says:

    Hi, i dont reallz know a lot about this things, but after doing all the steps but the bash keeps sendin pings.
    It says:
    ping: sendto: Host is down
    request timeout for icmp_seq 70

    the number increases as it keeps sending ping.

    Is this normal?

  29. Crates says:

    Here’s the thing about your script above, William… it doesn’t take into account the very nature of WiFi, which is such that a user may move from one environment to another at will.

    Instead, I’d recommend the user to execute “crontab -e” to edit their crontab file, and add the following:

    */30 * * * * netstat -nr | grep ^default | awk ‘ { print $2; } ‘ | xargs ping -i 30 -n -c 59

    This will automagically determine every 30 minutes what your gateway is, and only send pings to that address. If the gateway changes (and you haven’t lost connectivity in the last 30 minutes), you will start pinging the new one. If you DO lose connectivity, but re-establish it later, this still works.

    I noticed someone else put a similar comment about crontab above, but this one takes into account a key point from your post: hitting the closest node (the gateway) to not waste bandwidth.

    Anyways, thanks for a great post.

  30. Crates says:

    Oh… here’s a slightly different version that guarantees all output is redirected to /dev/null and the task executes in the background (put this in the crontab instead if you want to have this instead)…

    */30 * * * * netstat -nr | grep ^default | awk ‘ { print $2; } ‘ | xargs ping -i 30 -n -c 59 > /dev/null &

    … although, frankly, it doesn’t matter at all, since cron will execute this task in parallel with any other tasks, and without a stdout to push output to, it essentially goes to /dev/null anyways. It’s more for the obsessive-compulsive types.

  31. Crates says:

    Whoops! I was eating my own dog food here (to make sure that what I posted actually _worked_ … fancy that), and I realized that there isn’t enough specificity on the location of the netstat binary for it to work in cron.

    So, third time’s a charm…

    */30 * * * * /usr/sbin/netstat -nr | grep ^default | awk ‘ { print $2; } ‘ | xargs ping -i 30 -n -c 59 > /dev/null &

    Try that. Hope it works!

  32. Crates says:

    Grr… SO close… Same problem with the ping binary.

    Here’s another (hopefully final) revision:

    */30 * * * * /usr/sbin/netstat -nr | grep ^default | awk ‘ { print $2; } ‘ | xargs /sbin/ping -i 30 -n -c 59 > /dev/null &

    (Moderators, please feel free to merge my many comments. I don’t have the ability to edit them.)

  33. andrea says:

    Please help me! i try all the solutions in the web, and this is the last one… but when i press control + o or control x nothing happen in the terminal… then.. looks like this..
    #!/bin/bash
    ping -i 5 -n 192.168.178.1
    o
    x

    and i tried 1000 times, pressing control and 0 at the same time… and no… it won’t work… please help

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates