Automatically Reset Your Network Adapter on MacBook Wake with Sleepwatcher

There is an issue with USB-C hubs for MacBook having Ethernet adapter to miserably fail when MacBook goes to sleep.
The network connection is broken after the MacBook wakes. This is often the case with the Belkin USB-C Multimedia Hub.
The issue can be resolved by re-plugging the hub from its port. If you’re not fancy of doing this on every MacBook awake cycle, read on.

If your network adapter fails to reconnect when your MacBook wakes from sleep, you can automate resetting the adapter using sleepwatcher. This guide will not only help you set this up but also include logging using system’s log rotation to ensure that the log file remains manageable and doesn’t consume excessive disk space.

What You’ll Need

  • A MacBook with macOS
  • Homebrew (to install sleepwatcher and uhubctl)
  • Basic familiarity with Terminal

Step 1: Install Sleepwatcher and uhubctl

brew tap mvp/uhubctl https://github.com/mvp/uhubctl
brew install uhubctl
brew install sleepwatcher

Step 2: Identify Your Network Adapter

Use uhubctl to identify the USB hub and port of your network adapter. Simply run uhubctl without arguments in the Terminal app.
Note USB+Vendor ID from output string like:

  Port 4: 0203 power 5gbps U0 enable connect [0bda:8153 Realtek USB 10/100/1000 LAN 001000001]

Here, the USB and Vendor ID is 0bda:8153. We will use it in the reset network script to target specific device for power cycling.
Realtek chipset is often used in products like Belkin dock stations, so it may be exactly the value you need.

Step 3: Create the Network Reset and Logging Script

Create the script reset_network_adapter.sh to automate the adapter reset and logging:


I am using system’s Python 3, but you may want to use shebang of any Python 3 you installed, e.g. via brew.

Make the script executable, e.g. chmod +x /path/to/reset_network_adapter.py

Step 4: Configure Sleepwatcher

Set up the .wakeup file to execute your script upon waking:

echo "/path/to/reset_network_adapter.py" > ~/.wakeup
chmod +x ~/.wakeup

Now ensure that sleepwatcher service is running and enabled:

brew services restart sleepwatcher

Step 5: Verification

To verify that things are working, close the lid of your MacBook, then open it after a little while.
This will cause it to briefly sleep.

After this, you can check the log like so:

log show --predicate 'eventMessage contains "networkAdapterReset"' --info --last 10m

If everything is fine, you will see an output like so:

Timestamp                       Thread     Type        Activity             PID    TTL
2024-05-18 18:11:24.860085+0700 0xbbc379   Info        0x0                  39343  0    logger: [networkAdapterReset]: Starting network adapter reset script.
2024-05-18 18:11:25.082713+0700 0xbbc3a0   Info        0x0                  39356  0    logger: [networkAdapterReset]: Detected hub: 0-2, port: 4
2024-05-18 18:11:26.528828+0700 0xbbc3da   Info        0x0                  39361  0    logger: [networkAdapterReset]: Successfully power cycled hub 0-2, port 4.

Caveats

Rarely, but occasionally, the sleepwatcher service itself is acting buggy and doesn’t invoke the .wakeup script. In that case, simply launch the Terminal app and run ~/.wakeup to fix your network adapter.

Now, your MacBook will automatically reset the network adapter each time it wakes from sleep, with all actions logged. This automated setup enhances both connectivity and system maintenance. You no longer have to unplug your network adapter to have it consistently work.

You may also like...

Leave a Reply

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