Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A script to measure idle time on input devices' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to measure idle time on input devices
Authored by: Jonas Lundberg on Apr 05, '04 02:45:54PM

This does not work in 10.2.8 for some reason. There might be some difference in the output format of the ioreg command but I haven't really checked that.



[ Reply to This | # ]
A script to measure idle time on input devices
Authored by: babbage on Apr 09, '04 11:09:43AM

Yes, the ioreg subsystem is keeping track of time differently between 10.2.x and 10.3.x. Observe a 10.2 machine:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.2.8
BuildVersion:   6R73
$ sleep 2; ioreg -c IOHIDSystem | grep HIDIdleTime|sed 's#.*"HID#"HID#'
"HIDIdleTime" = <0000396da619284f>
"HIDIdleTime" = <0000396da64614ca>
"HIDIdleTime" = <0000396da67986cd>
"HIDIdleTime" = <0000396da71ab313>
$

And here's the same commands on a 10.3 machine:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.3.3
BuildVersion:   7F44
$ sleep 3; ioreg -c IOHIDSystem | grep HIDIdleTime|sed 's#.*"HID#"HID#'
"HIDIdleTime" = 2930089953
"HIDIdleTime" = 2932857364
"HIDIdleTime" = 2935033969
"HIDIdleTime" = 2936912079
"HIDIdleTime" = 2939724019
"HIDIdleTime" = 2941758207
"HIDIdleTime" = 2943638209
"HIDIdleTime" = 2952352977

So on 10.3, the HIDIdleTime is pretty recognizable: move the decimal to the left 9 times and you get 2.9...., which is about right. On 10.2 on the other hand, the value seems to be about the same no matter how many seconds I sleep before letting the command run, and there's all those letters suggesting that the number is probably a hexadecimal value. Someone would have to figure out how to convert those numbers into the decimal representation in seconds, but I'm not sure how to do it yet.

For those that are interested, I've got a modified version of the script going:

$ cat ~/bin/osx_idle_time.sh
#!/bin/sh

echo -n "`date`: `uname -n` has been idle for "

secs=`ioreg -c IOHIDSystem | \
      awk '/HIDIdleTime/ {print $NF/1000000000; exit}' | \
      sed 's#\(\.[0-9][0-9]\)[0-9]*#\1#'`

echo "$secs seconds"

Which when run (on a remote machine, in this case), produces output like this:

$ ssh myhomemac.dyndns.org "~/bin/osx_idle_time.sh"
cdevers@myhomemac.dyndns.org's password: 
Fri Apr  9 11:06:42 EDT 2004: Myhomemac.local has been idle for 15650.6 seconds

Nice! Thanks for the idea!

---
--
DO NOT LEAVE IT IS NOT REAL

[ Reply to This | # ]

10.2
Authored by: mzs on Sep 05, '06 11:53:13AM
This should work in 10.2, it used to but I have made changes to the script since then:

#!/bin/sh

# Display idle time

foo='
/^[ |]*"HIDIdleTime" = / {
        # 10.2 and earlier uses CFData, later uses CFNumber
        if (substr($NF, 1, 1) == "<") {
                fmt="scale = 9; obase = 10; ibase = 16; %s / 3B9ACA00"
                v=toupper(substr($NF, 2, 16))
        } else {
                fmt="scale = 9; %s / 1000000000"
                v=$NF
        }

        line=sprintf(fmt, v)
        cmd="/bin/sh -c '\''echo \"" line "\" | bc'\''"
        cmd | getline line
        close(cmd)

        fmt="+%H:%M:%S"
        i=index(line, ".")
        if (i)
                fmt=fmt substr(line, i, 10)

        fmt=" '\''" fmt ", " line "s'\''"
        cmd="date -nur " line fmt
        cmd | getline line
        close(cmd)

        print line
        exit 0
}
'

ioreg -c IOHIDSystem | awk "$foo"
This gives nicer output like this:

% idletime 
00:52:04.493391248, 3124.493391248s
(I was ssh-ed in.)

[ Reply to This | # ]