I have a script that needs to run every so often, but only if the screensaver is active or if the screen is asleep (to make sure nobody is actively using the computer first). I need to have a way to determine whether the screen is asleep from the command line. (One-liner preferred)

I'm using "ps -acxw | grep ScreenSaverEngine | grep -v grep" to determine if the screensaver is running, which always worked fine in previous OS versions, but in Mountain Lion, the screensaver is disabled when the screen goes to sleep, so I need a way to determine that as well now.

Help is appreciated...

  • I too have been trying to find a way to query the display sleep state from the command line. Coming up empty so far. :(
    – ipmcc
    Sep 26, 2013 at 12:25
  • 1
    If this code returns a 4 then the screen is awake, if it returns a 1 then the screen is asleep. ioreg -n IODisplayWrangler -r IODisplayWrangler -w 0 | sed -n 's/^.*"CurrentPowerState"=\([0-9]\+\).*/\1/p' Feb 23, 2016 at 18:01

3 Answers 3


Though not directly answering your question this line will give you the time in seconds since the last user interaction which comes from what OS X considers to be an idle user. (independently of screen-savers or black displays.)

echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
  • Hmmm... this might be a good alternative to sensing the Screen Saver or Display Sleep status. Thanks. Still would like the original answer as well, but this might be a good alternative.
    – G Piper
    Dec 10, 2012 at 4:23
  • Wait... your command gives me this error: "Illegal variable name"
    – G Piper
    Dec 10, 2012 at 4:25
  • Did you copy/paste the command? Mixing up the different kinds of quotes and backticks will break it. Just in case copying doesn't work here use this paste bin pastebin.com/7JrPNxYU
    – MacLemon
    Dec 10, 2012 at 12:47
  • Yes, I cut and pasted it. Tried the cut-and-paste bin too... no luck. Same error. I've been cutting and pasting for 18 years too. (-; It doesn't seem to work on 10.8.2? I can see what you're trying to do with it, too... I may try to roll my own from that, but having difficulty.
    – G Piper
    Dec 10, 2012 at 16:35
  • It works perfectly on my machines running 10.6.8 (bash) as well as 10.8.2 (zsh). (I don't have a Lion Mac at hand.) You could also try to derive from echo $(( $(ioreg -c IOHIDSystem | grep HIDIdleTime | sed -e 's/[^0-9]//g') / 1000000000 ))
    – MacLemon
    Dec 10, 2012 at 16:57

I think I've figured it out. This does it for me:

ioreg -n IODisplayWrangler | grep -i IOPowerManagement | perl -pe 's/^.*DevicePowerState\"=([0-9]+).*$/\1/'

As for the number that this returns, in my case it appears to be in the range of 0-4. I don't know what each number specifically means for sure, but when the display is "on", I get 4, and when the display has been put to sleep by idle I get 0. I assume the in-between states are for dimming, etc, but I don't have a laptop here, so I'm not sure.

  • This does work and confirm. 4 indicates on, 0 for off. Tried on a laptop, did not see the other states. Assuming anything larger then 0 is on maybe a safe bet. (have yet to see a value other then 4 or 0)
    – Infamy
    Mar 17, 2016 at 23:14

Thanks, based on the tip by ipmcc, I've created the one-liner that outputs a boolean value.

echo $(ioreg -n IODisplayWrangler | grep -i IOPowerManagement | perl -pe 's/^.*DevicePowerState\"=([0-9]+).*$/\1/')/4 | bc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .