使用sleepwatcher解决mac合盖休眠掉电问题

通过 homebrew 安装 sleepwatcher,是最简单的方式:

brew install sleepwatcher

设置软件服务自启动

brew service start sleepwatcher

SleepWatcher默认执行wake事件配置文件是在~/.wakeup,执行sleep事件配置文件是在~/.sleep所以我们将下面两个脚本复制到用户目录下

nano ~/.sleep
#!/bin/bash
 
status=$(networksetup getairportpower en0 | grep -Ei "On$")
 
if [ -f /tmp/wifi.on ]; then
    rm /tmp/wifi.on
fi
 
if [ "" !=  "$status" ]; then
    echo "`date` -- WiFi is On ... storing statement" >> /tmp/sleep.log
    
    touch /tmp/wifi.on
    networksetup setairportpower en0 off
fi

nano ~/.wakeup

if [ -f /tmp/wifi.on ]; then
    echo "`date` -- Enablig WiFi" >> /tmp/sleep.log
    sleep 3
    networksetup setairportpower en0 on
fi

最后执行,令上面两个脚本生效,省得重启

cp /usr/local/Cellar/sleepwatcher/2.2/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist