A script to measure idle time on input devices

Apr 05, '04 09:35:00AM

Contributed by: Thom

I needed a quick shell script that would tell me how long the input devices attached to a system had been idle; I needed to perform maintenance, but could not when a user was interacting with it (it's a museum exhibit). Drew Thaler of Multisolar came up with the following. You can put it in your personal ~/bin directory, and remember to chmod it 755.

#!/bin/sh
echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/
 !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
NOTE: Remove the line break from the above echo line (i.e. ...HIDIdleTime/!{...); it's been split here for a narrower display width.

Obviously, running this script directly from the command line will always evaluate to zero, but you can test it with a sleep command:
%> sleep 4; system_idle_time
4
Replace system_idle_time with whatever you named the script, of course. The returned value is in seconds; adjust the divisor accordingly for other time increments.

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040330161158532