Turn off Wi-Fi when macOS goes to sleep
08 Mar 2021 - Help improve this postWhen I am on a network that’s paid by the hour you want to disable your Wi-Fi when you are not using your mac. To do this I listen for when the mac goes to sleep and disable the Wi-Fi.
- Install sleepwatcher with homebrew:
brew install sleepwatcher - I have my scripts living in
~/Developer/scripts(create that folder withmkdir -p ~/Developer/scripts) -
Create a script at
~/Developer/scripts/sleepscript.sh:networksetup -setairportpower Wi-Fi off - Make your script excutable in your Terminal with
chmod +x ~/Developer/scripts/sleepscript.sh - Test your script with
/usr/local/sbin/sleepwatcher --verbose --sleep ~/Developer/scripts/sleepscript.sh --wakeup /path/to/your/wakeupscript -
Create a Launch Agent at
~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>de.bernhard-baehr.sleepwatcher</string> <key>ProgramArguments</key> <array> <string>/usr/local/sbin/sleepwatcher</string> <string>-V</string> <string>-s ~/Developer/scripts/sleepscript.sh</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>StandardOutPath</key> <string>~/Developer/scripts/sleepscript-output.log</string> <key>StandardErrorPath</key> <string>~/Developer/scripts/sleepscript-errors.log</string> </dict> </plist>Just save it to
~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plistDon’t change
-sto--sleepbecause somehow that didn’t work. - Enable and start the Launch Agent
launchctl load -w ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist - Test is while putting your mac to sleep. No luck? Check your logs in
~/Developer/scripts/
Happy coding! – Found a mistake or a typo? Please submit a PR to my GitHub-repo.