【问题标题】:Sleepwatcher on OS X 10.11 not executing script on wakeOS X 10.11 上的 Sleepwatcher 未在唤醒时执行脚本
【发布时间】:2017-01-20 09:52:51
【问题描述】:

在 OS X 10.11 上安装 Sleepwatcher 2.2 并通过 LaunchD 作为代理启动它。

它可以正常启动并显示在活动监视器中。

但是,我希望它在计算机唤醒时触发 python 脚本。

我的安装命令如下。

sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin
sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8
sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher/config/rc.sleep /etc
sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher/config/rc.wakeup /etc
sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher/config/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist /Library/LaunchAgents

chmod +x /etc/rc.sleep
chmod +x /etc/rc.wakeup
chmod +x /usr/local/bin/test.py

我的 rc.wakeup 文件如下。

#!/bin/sh
/usr/local/bin/python3 /usr/local/bin/test.py

在终端窗口通过输入以下内容执行 Sleepwatcher 时,它似乎可以工作。

/usr/local/sbin/sleepwatcher --verbose --wakeup /usr/local/bin/test.py 

但是,当尝试在 LaunchD 下将其作为启动项运行时,执行我的 python 脚本似乎不起作用。

我已经搜索了所有内容,但无法弄清楚为什么它在 LaunchD 中启动时不起作用。

有人遇到过这种问题吗?

提前致谢。

【问题讨论】:

  • 是的,我将命令输入为 chmod +x /etc/rc.sleep。我不知道为什么上面有一个额外的空间。
  • 您似乎缺少 plist 的注册,这是启动 sleepwatcher 运行以及在睡眠和唤醒事件发生时实际调用 /etc/rc.sleep/etc/rc.wake 脚本所必需的。
  • 嗨Petesh,我没有执行launchctl load /Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist,而是重新启动了服务器。这也应该把它捡起来。您是否知道是否留下了任何日志,以便我可以查看在唤醒事件期间加载或执行脚本期间可能引发错误的原因?
  • 原来上面没有问题,问题出在使用的文件之一上。

标签: python macos unix terminal macports


【解决方案1】:

我遇到了类似的问题,所以我使用另一个名为Hammerspoon 的开源工具采取了不同的方法。它可以提供 MacOS 上的一堆东西的自动化,包括睡眠/唤醒事件。通过将以下内容添加到 Hammerspoon 的 ~/.hammerspoon/init.lua(或创建一个“spoon”)脚本中复制 sleepwatcher 的功能非常简单,该脚本在机器唤醒或睡眠时触发并调用相应的唤醒和睡眠脚本(例如 /Users/username/scripts - 确保 username 已更改)来自 sleepwatcher:

function caffeinateWatcher(eventType)
    if (eventType == hs.caffeinate.watcher.systemWillSleep or
            eventType == hs.caffeinate.watcher.systemWillPowerOff) then
            print ("WillSleep...")
            -- Execute sleep script
            hs.task.new("/Users/username/scripts/rc.sleep", nil):start()
    elseif (eventType == hs.caffeinate.watcher.systemDidWake) then
            print ("Woken...")
            -- Execute wake script
            hs.task.new("/Users/username/scripts/rc.wake", nil):start()
    end
end

sleepWatcher = hs.caffeinate.watcher.new(caffeinateWatcher)
sleepWatcher:start()

请注意,如果您希望 Hammerspoon 启动 shell 脚本,您需要确保它们以标准 bash shell 标头 #!/bin/bash 开头。

【讨论】:

  • 我正在使用您的示例在我的公司上运行一些脚本。谢谢。这有点困难,因为在从脚本启用一些日志之前,不清楚他能找到或找不到什么路径。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多