是否可以编写一个 python 程序(我认为它将作为守护进程运行)来检测 osx 何时进入睡眠状态以及何时从睡眠中恢复?
如果听起来我没有对此进行研究,我深表歉意——我已经脱离了我的舒适区,并且不确定我是否需要从 python 委托给用 C 编写的可以报告这一点的东西,或者那是不必要的。
IOKit 中有一些工具可以做到这一点——具体来说,注册的回调IORegisterForSystemPower()
将在系统睡眠之前和唤醒之后被调用。
您可能想查看bb 的sleepwatcher
daemon,它可以在各种事件发生时调用您指定的命令,包括系统睡眠/唤醒,以及各种其他事件(显示睡眠/唤醒、系统空闲、关机...) .
Cocoa 开发者可以听听NSWorkspaceDidWakeNotification
- (void) receiveSleepNote: (NSNotification*) note
{
NSLog(@"receiveSleepNote: %@", [note name]);
}
- (void) receiveWakeNote: (NSNotification*) note
{
NSLog(@"receiveWakeNote: %@", [note name]);
}
- (void) fileNotifications
{
//These notifications are filed on NSWorkspace's notification center, not the default
// notification center. You will not receive sleep/wake notifications if you file
//with the default notification center.
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
selector: @selector(receiveSleepNote:)
name: NSWorkspaceWillSleepNotification object: NULL];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
selector: @selector(receiveWakeNote:)
name: NSWorkspaceDidWakeNotification object: NULL];
}
此示例代码摘自此处的 Apple 文档:注册和取消注册睡眠和唤醒通知