Wi-Fi network not connecting after sleep

Sometimes even though a wireless network is available, after waking from sleep OS X may not connect to it. Here is a workaround that will keep the system connected.

Recently I touched on several approaches for addressing Wi-Fi issues in OS X, stemming from configuration problems to Wi-Fi signal quality for addressing dropouts.

These methods are good for situations when even though the Wi-Fi signal drops, the radio still attempts to find a connection. But there may be some instances when the radio stops searching even though a Wi-Fi network is available. Often this occurs when the system wakes from sleep, but sometimes it can happen when the system changes networks.

If this happens, then one option is to remove the airport configuration file, which contains all the information for how the system should handle the various Wi-Fi networks that you use. This file is located in the /Macintosh HD/Library/Preferences/SystemConfiguration/ folder and is called "com.apple.airport.preferences.plist." Remove this file and then restart your computer, then join your Airport network again to see if that helps the situation.

While a fresh Airport configuration should help, in some instances the situation may resurface again either immediately during or after a period of using the system. In these cases, the system will still connect if you manually select your network, turn Airport off and on again, or otherwise alter your Airport connection. As a result, you can create a workaround that will perform these actions for you, which will keep the system connected and prevent you from needing to remove configuration files or manually select your network.

This approach takes advantage of the system launcher "launchd" to have it monitor when the system does not have a proper network connection, and then has it run a small script that will tell Airport to re-establish its connection.

Wi-Fi network name
The Wi-Fi network name can be seen here. In most cases it should be "Wi-Fi." Screenshot by Topher Kessler

The first step to this procedure is to create the script that will re-establish the connection, so to do this follow these steps:

  1. Look up network information
    First you will need to know what the name of your Wi-Fi connection is. By default it is called "Wi-Fi," but some people may have renamed these, especially when performing some network troubleshooting steps. Therefore, go to your Network system preferences and see what the name of your Airport connection is, which will be listed in the connection list next to Ethernet, FireWire, VPNs, Bluetooth, and other connection options.

  2. Create the script file
    Open TextEdit and create a new document. Then press Shift-Command-T to make it a "Plain Text" document (this can also be done in the Format menu). After doing this, copy and paste the following text into the document. In this case, the name of the network connection is "Wi-Fi," but if your connection is named differently, then replace the "Wi-Fi" text here with the name of your connection:

    /usr/sbin/networksetup -setnetworkserviceenabled Wi-Fi off
    /usr/sbin/networksetup -setnetworkserviceenabled Wi-Fi on
    sleep 10

    When done, save the file with the name "connectairport.sh" in the /Macintosh HD/Library/Scripts/ folder, or somewhere similar in the global library folder. Placing it in this folder ensures it will be accessible to other accounts on the system, and also that it will run and keep Airport connected even if you are logged out.

  3. Ensure the script is executable
    Programs and scripts in OS X cannot be run unless they are set to be executable. Therefore, in the Terminal run the following command to ensure the file can be run:

    chmod +x /Library/Scripts/connectairport.sh

    If the command gives you an error saying "permission denied" or "access denied," then type the word "sudo" followed by a single space before the command and supply your password when prompted.

At this point the script is set up to run in a globally accessible area. When run it will first turn off the Wi-Fi radio and then turn it back on, followed by waiting 10 seconds. When this happens, the Wi-Fi network should re-establish its connection automatically with the preferred Wi-Fi network.

The next step is to create a launch agent file for running this script whenever the system does not have an active network connection, which can be done by opening TextEdit and again setting the new document to be plain text. Then paste the following text into it:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
          <false />

Save the script file with the name "local.connectairport.plist" to the /Macintosh HD/Library/LaunchAgents/ folder, and then activate it either by restarting or by running the following command in the Terminal (provide your password when prompted):

sudo launchctl load /Library/LaunchAgents/local.connectairport.plist

When this is done, the script is all set up and ready to go. The launch agent script will run the script you previously created, and keep it alive only when the "NetworkState" is "false," meaning that the system does not have an IP address on an active connection. Now when your system wakes from sleep and does not automatically connect, the system will turn the airport radio off and then back on, which will spur a connection to your preferred Wi-Fi network.

The script will wait 10 seconds before trying again, and once a connection is made it will be silenced and no longer run. If you need a longer time period, then you can edit the first script and change the interval from 10 to another value (15, 20), based on your system's needs. Ten seconds should be fine for most situations, but if your system does not connect and you see the radio turning off and on every 10 seconds, then you should change the interval.

Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Featured Video
This content is rated TV-MA, and is for viewers 18 years or older. Are you of age?
Sorry, you are not old enough to view this content.

iPhone 6S chip controversy over battery life

Not all new iPhones have the same processor chip, but Apple says differences in performance are minimal. Apple also pulls ad-blocking apps over privacy concerns, and Netflix raises its price again.

by Bridget Carey