原贴地址:Sleepwatcher : Run Script on Sleep

Sleepwatcher 是一个可以再mac睡眠和唤醒时自动执行命令行脚本的工具,下文两部分分别描述了这个工具的安装与设置方法,在实际的使用中不仅可以执行bash脚本,而且可以执行Applescript命令,使用起来十分方便。


Sleepwatcher is a Mac OS X background daemon that can trigger a script to run when various power related events occur, such as the laptop sleeping or waking up, the screen dimming, or power adapter being connected or disconnected.

On my personal laptop, I have an AppleScript set up within sleepwatcher that mutes the audio each time the laptop goes to sleep. This is to prevent accidental audio playing during meetings and classes.

Setup

Below is setup script for sleepwatcher that I have written that installs it automatically as a system wise daemon.

#!/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"
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.plistsleep 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

The script begins with deleting any old version of sleepwatcher on the computer. It them moves onto downloading the package and copying the files into the appropriate places. It loads the launch daemon then creates two files, /etc/rc.sleep and /etc/rc.wakeup. These get run at their respective times, when the laptop is going to sleep or waking up.

Configuration

My personal /etc/rc.wakeup script is shown below. All it does is run the short AppleScript command of setting the volume to zero. To get this functionality on your computer, just copy the code below into your own /etc/rc.wakeup.

#!/bin/sh
osascript -e 'set volume 0'

Other possible uses for sleepwatcher include updating a DNS record on wakeup, opening up specific applications for a user, and of course running another script of the user’s choice.

Sleepwatcher:Mac睡眠与唤醒自动执行命令相关推荐

  1. Mac OS开机启动自动执行命令行

    Mac OS开机启动自动执行命令行 公司的打包服务器升级需要频繁重启,或者物业断电而必须关机时,下次开机都需要手动启动一些服务,这样就显得非常麻烦,故需要进行配置使得Mac开机后自动启动服务. 首先编 ...

  2. python脚本连接交换机,自动执行命令。

    python脚本连接交换机,自动执行命令. python脚本连接交换机,自动执行命令,查询交换机端口的学习mac地址,自动写入txt中.设置定时器,每xx秒执行一次,自动对比上次返回结果,TXT中保存 ...

  3. java 案例 一千例,【发现问题、解决问题系列案例分享(二十一)】 一个自动执行命令,每天节省一千分钟!...

    原标题:[发现问题.解决问题系列案例分享(二十一)] 一个自动执行命令,每天节省一千分钟! 编辑 | 林采依 撰文 |钱奕昀 嘉定局 在用户信息安全检测中,营业厅电脑因受理业务时需使用安真通 无纸化拍 ...

  4. Xshell登录后自动执行命令

    目录 专栏说明 一.Xshell软件介绍 二.Xshell登录后自动执行命令 1.软件下载: 2.Xshell登录后自动执行命令 专栏说明 本专栏主要介绍windows及linux下嵌入式开发工作者常 ...

  5. Linux开机后自动执行命令或脚本

    文章目录 说明 应用场景 涉及文件 步骤 1.添加开机执行指令 2.编写脚本 3.测试效果 自动化编辑工具 脚本 效果 已存在的脚本 不存在的脚本 说明 应用场景 应用场景:开机后自动执行命令或脚本 ...

  6. linux开机自动执行命令或自动启动程序(rc.local)

    linux开机的最后会执行/etc/rc.local,因此可以在此脚本里面添加shell命令自动执行或者自动启动某个进程. 比如 自动输出信息: #!/bin/sh -e # # rc.local # ...

  7. BATCH+VBS脚本自动执行命令

    打开cmd,然后执行run.bat s 1就会执行tmpsw.vbs的内容,1是作为id传进去的参数. run.bat: @echo off set type=%1 set id=%2 :打开一个te ...

  8. linux开机自动执行命令

    /etc/rc.local是/etc/rc.d/rc.local的软链,像windos的快捷方式 开机自动执行的命令在此文件尾部exit 0 之前输入即可 给/etc/rc.local文件添加执行权限 ...

  9. 详解在 Linux 启动时,如何自动执行命令或脚本

    2019独角兽企业重金招聘Python工程师标准>>> 我一直很好奇,在启动 Linux 系统并登录的过程中到底发生了什么事情.按下开机键或启动一个虚拟机,你就启动了一系列事件,之后 ...

最新文章

  1. 使用iText库创建PDF文件
  2. Containers vs Serverless:怎么选择?
  3. supost a second hand marketing place at stanford
  4. BeanUtils.copyProperties 需要getset方法支持
  5. 关于swift5以上的SnapKit框架的入门教程使用
  6. 计算机网络又称为分时多用户,7计算机网络原理期末复习试卷A卷.doc
  7. 查询:使用多表连接查询数据
  8. Vivado 2019.1 生成bit文件报错解决
  9. 如何防止盗号 使用windows自带的 屏幕键盘 OSK
  10. php学习之路:WSDL详细解释(两)
  11. 算法笔记_面试题_22. 图与搜索(BFS/DFS)相关_模板及示例十几道
  12. 完全免费绝地求生吃鸡罗技宏
  13. Wpe工作原理和教程-以传奇为列
  14. python应对反爬虫策略_python解决网站的反爬虫策略总结
  15. linux bridge vlan,Linux Bridge实现Vlan
  16. ArcGIS Desktop 10.1 的系统自带的 prj 文件的去哪了?
  17. 来一起看看抖音对于功耗优化是怎么做的;抖音Android性能优化实战
  18. LR性能测试Web Page Breakdown分析篇
  19. 集成安装光盘制作教程(http://bbs.deepin.org/viewthread.php?tid=1170144)
  20. Binder基石-Parcel

热门文章

  1. 摸头GIF在线生成HTML源码
  2. WindowsErrorCode
  3. MySQL无备份未开启二进制的日志,恢复数据库的数据
  4. 三亚:严查“先登记支付房款、后补交社保或个税”行为
  5. 关于先验概率和后验概率的通俗解释。
  6. 奇瑞无界Pro来啦,身虽小内心很强大
  7. MySQL亲手编写万文复习
  8. 字符类型回文序列判断方法,寻找多个子字符串中的回文序列
  9. 机器人学Robotics学习资料 | 我的SLAM入门路线分享
  10. Python快速入门——Day4