Skip to content

Instantly share code, notes, and snippets.

@cuibonobo
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save cuibonobo/875f94cdcd1c5ba91a63 to your computer and use it in GitHub Desktop.

Select an option

Save cuibonobo/875f94cdcd1c5ba91a63 to your computer and use it in GitHub Desktop.
How to detect that a person has been idle on Mac OS X
#!/bin/sh
# Output is given in seconds. Taken from http://stackoverflow.com/a/8659356 and *originally* from
# http://hints.macworld.com/article.php?story=20040330161158532 (posted in 2004!!)
echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
@cuibonobo
Copy link
Author

In my travels I have found 2 other ways to parse the output of ioreg -c IOHIDSystem. The first uses awk to parse the output:

ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'

The second uses a combination of grep and awk:

echo $((`ioreg -c IOHIDSystem | grep -m 1 'HIDIdleTime' | awk '{print $NF}'` / 1000000000))

While they are easier to read than the original, the sed still beats the first one on speed and the second one on the number of pipes (and arguably on speed also, but the margin is much closer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment