mac唤醒后运行脚本:Sleepwatcher

Sleepwatcher是一个Mac OS X后台守护程序,可以在各种电源相关事件发生时触发脚本运行,例如笔记本电脑正在睡眠或唤醒,屏幕变暗或电源适配器连接或断开连接。

在我的个人笔记本电脑上,我在Sleepwatcher中设置了一个AppleScript,可在每次笔记本电脑进入睡眠状态时将音频静音。这是为了防止在会议和课堂上意外播放音频。

建立

下面是我编写的sleepwatcher安装脚本,它将自动安装为系统智能守护程序。

(sleepwatcher.sh)下载
#!/bin/bash

# acquire sudo at the beginning
sudo -v

# Keep-alive: update existing `sudo` time stamp until `.osx` has finished
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &

# unload launch agents
sudo launchctl unload /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist 2>/dev/null
launchctl unload ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist 2>/dev/null

# remove plist launchagents
sudo rm -f /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist
rm -f ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

# remove executable and man files
sudo rm -f /usr/local/sbin/sleepwatcher
sudo rm -f /usr/local/share/man/man8/sleepwatcher.8

# download sleepwatcher package, untar, and cd into directory
# curl --remote-name "http://www.bernhard-baehr.de/sleepwatcher_2.2.tgz"
curl --remote-name "http://cdn1.xnb.me/mac/shell/sleepwatcher_2.2.tgz"
tar xvzf sleepwatcher_2.2.tgz 2>/dev/null
cd sleepwatcher_2.2

# create folders necessary for installation
sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8

# move files into installation folders
sudo cp sleepwatcher /usr/local/sbin
sudo cp sleepwatcher.8 /usr/local/share/man/man8
sudo cp config/de.bernhard-baehr.sleepwatcher-20compatibility.plist /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist

sleep 1

# load launch agent
sudo launchctl load -w -F /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist

# create script in local user directory and make them executable
sudo touch /etc/rc.wakeup
sudo touch /etc/rc.sleep
sudo chmod +x /etc/rc.sleep /etc/rc.wakeup

 

该脚本首先删除计算机上的任何旧版本的sleepwatcher。他们转而下载软件包并将文件复制到适当的位置。它加载启动守护进程然后创建两个文件,/etc/rc.sleep并且/etc/rc.wakeup。当笔记本电脑要睡觉或醒来时,它们会在各自的时间运行。

我的个人/etc/rc.wakeup剧本如下所示。它所做的只是运行简短的AppleScript命令将音量设置为零。要在您的计算机上获得此功能,只需将以下代码复制到您自己的计算机上/etc/rc.wakeup

/etc/rc.wakeup
#!/bin/sh
osascript -e 'set volume 0'

 

sleepwatcher的其他可能用途包括在唤醒时更新DNS记录,为用户打开特定应用程序,当然还可以运行用户选择的另一个脚本。

/*********************/

我的黑苹果更新系统后,睡眠唤醒后网卡不能联网,于是我在/etc/rc.wakeup里加入以下代码

#!/bin/sh
ifconfig en0 down
ifconfig en1 down
ifconfig en0 up
ifconfig en1 up

让网卡重启一遍。希望对遇到我一样的总是的朋友有帮助。

 

转载自:http://tyhoffman.com/blog/2013/09/sleepwatcher-power-event-driven-automation/

版权声明:
作者:xiaoniba
链接:https://blog.xiaoniba.com/2018/02/21/mac%e5%94%a4%e9%86%92%e5%90%8e%e8%bf%90%e8%a1%8c%e8%84%9a%e6%9c%ac%ef%bc%9asleepwatcher/
来源:小泥吧的博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>