Mac Wi-Fi Dropping? Use a Simple Keepalive Bash Script to Maintain Wireless Connection
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”:
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:
- Paste in the following, be sure to replace the IP with your own routers:
- Hit Control+O to Save the contents of keepalive.sh
- Hit Control+X to exit from nano
nano keepalive.sh
#!/bin/bash
ping -i 5 -n 192.168.1.1
3) Run the Wi-Fi Keepalive Bash Script
- Back at the command line, we have to make the script executable, we do this with:
- Now to run the keepalive script, we type:
chmod +x keepalive.sh
./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:
- WiFi Dropping in OS X Lion? Here are Some Wireless Troubleshooting Solutions
- Guide to Troubleshooting Wireless Problems on your Mac
- Fixed dropping wireless Airport connection problem in Mac OS X 10.6 Snow Leopard
- Simple fix for wireless dropping problems with Mac OS X 10.6.3
Good luck!
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
he forgot to say press enter after control+o beacuse it’s asking if you want to name it that at the bottom. so just hit enter, and it will say wrote 2 lines or 3 lines. then hit control+x.
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.)
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!
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.
This is a great idea, thanks for this!
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.
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?
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?
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?
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?
use screen
screen [command]
then to resume
screen -r pid
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
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.
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.
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.
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?
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
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.
Ive posted this to the other thred “https://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.
No this one did not work, but thanks and I am glad it worked for you
Thanks Paul,
You know your stuff. It worked!
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.
Try hitting return after Control+O to accept the script name, then Control+X
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.
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
Keychain cleanup (all wi-fi passwords) solved the problem.
MBP 2006
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
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?
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.
I’m using (the simpler) Network Utility to do that :)
[…] 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 […]
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.
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.
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.
8.8.8.8 and 8.8.4.4 are the Google Public DNS servers.
4.2.2.6 also works well.
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!
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.
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.
Thanks for that Workaround.
But just in case how can I revert back to the original state?
Thanks for your reply.
DJay
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.
People should just do a clean install… that will avoid that problem.
Not when you purchase a MacBook Air with Lion installed, like mine, and experience the issue.
It seems like quite a few MacBook Air owners are having the problem, let us know if the keepalive script works for you
I agree with you and would recommend a clean install.
I try clean install but it does not help .
Its a hardware issue with Macs wifi cards or whatever ones they use. I’ve done many clean installs and always have to reference back to this page for the script to stop the constant dropping every 1-5 minutes.
I use iMac mid 2011 21.5 inch screen