OS X time until (system/display/disk) sleep?

▼魔方 西西 提交于 2019-12-04 15:05:56

问题


Does anyone know of a way to query OS X in order to find out how much time is actually left before it enters either system sleep or activates the display sleep (or even disk sleep), either via the command line, or any other method (Ruby or Objective-C for instance)?

I thought something like pmset via the command line might have offered this information, but it appears to only show and update what the current settings are, rather than allow feedback of where in the cycle the OS currently is.

My requirement is that I currently have a Ruby script that I'd like to run only when I'm not using the machine and a simple 'whatcher script' would allow this, but what to 'watch' is the thing I need a little help with.

It seems like there should be a simple answer, but so far I've not found anything obvious. Any ideas?


回答1:


Heres a bash script to show the commands, which are a little complicated. The idea is that the user sets the system sleep time in the energy saver preference pane. The system will actually go to sleep when no device attached to the computer has moved for that time. So to get the time until the system goes to sleep we need the difference between those 2 results.

#!/bin/bash

# this will check how long before the system sleeps
# it gets the system sleep setting
# it gets the devices idle time
# it returns the difference between the two
#
# Note: if systemSleepTimeMinutes is 0 then the system is set to not go to sleep at all
#

systemSleepTimeMinutes=`pmset -g | grep "^[ ]*sleep" | awk '{ print $2 }'`

if [ $systemSleepTimeMinutes -gt "0" ]; then
    systemSleepTime=`echo "$systemSleepTimeMinutes * 60" | bc`
    devicesIdleTime=`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'`
    secondsBeforeSleep=`echo "$systemSleepTime - $devicesIdleTime" | bc`
    echo "Time before sleep (sec): $secondsBeforeSleep"
    exit 0
else
    echo "The system is set to not sleep."
    exit 0
fi


来源:https://stackoverflow.com/questions/3502138/os-x-time-until-system-display-disk-sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!