【问题标题】:Detect whether host is in "sleep" or "awake" state in macOS在 macOS 中检测主机是处于“睡眠”还是“唤醒”状态
【发布时间】:2017-03-06 20:56:22
【问题描述】:

上下文

我正在为 macOS 开发一项服务,该服务在后台运行并定期执行某些任务。 该服务在崩溃或自动更新时由launchd 自动重启。

我注意到,当机器处于睡眠状态时,它仍会在每小时 1 或 2 分钟内执行程序。

目标

我的目标是在主机处于睡眠状态时中断这些任务的执行。 为此,我正在处理系统发送应用程序的睡眠和唤醒通知 (Apple Docs: Registering and unregistering for sleep and wake notifications)。

问题

我发现的问题是,如果在系统处于睡眠状态时更新服务或重新启动服务,则服务启动并假定状态为“唤醒”。 因此它会尝试运行它不应该运行的东西。

我想知道是否有办法查询系统是处于“睡眠”还是“唤醒”状态。

【问题讨论】:

标签: macos sleep-mode


【解决方案1】:

您可以运行system_profiler SPDisplaysDataType 并解析输出以提取Display Asleep 状态。

如果没有找到,则系统处于唤醒状态。

Graphics/Displays:

  NVIDIA GeForce GT 750M:

    Chipset Model: NVIDIA GeForce GT 750M
    Type: GPU
    Bus: PCIe
    PCIe Lane Width: x8
    VRAM (Total): 2 GB
    Vendor: NVIDIA (0x10de)
    Device ID: 0x0fe9
    Revision ID: 0x00a2
    ROM Revision: 3776
    Automatic Graphics Switching: Supported
    gMux Version: 4.0.8 [3.2.8]
    Metal Family: Supported, Metal GPUFamily macOS 1
    Displays:
      Color LCD:
        Display Type: Built-In Retina LCD
        Resolution: 2880 x 1800 Retina
        Framebuffer Depth: 24-Bit Color (ARGB8888)
        Mirror: Off
        Online: Yes
        Display Asleep: Yes
        Automatically Adjust Brightness: Yes
        Connection Type: Internal

【讨论】:

  • 这个不错,刚试了一下。但这只是告诉你 display 正在休眠,而不是整个 mac?
  • 奇怪的是,即使您关闭所有外接显示器并合上笔记本电脑盖,Display Sleep 状态似乎也不会被检测到(除非操作系统也被暂停/锁定)。
猜你喜欢
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2010-12-06
  • 2011-08-02
  • 1970-01-01
  • 2012-10-08
  • 2016-03-05
相关资源
最近更新 更多