System idle time - how to retrieve?

  1. I have a perl script. I want to know how long, preferably in seconds, the system has been idle (i.e. no user interaction - the time screen saver activation is based upon).

    I am ok with using a osascript breakout to retrieve it, but I can't figure out how... Anyone can help me?

    Sandman[.net]

  2. Previously, Sandman wrote:

    I have a perl script. I want to know how long, preferably in seconds, the system has been idle (i.e. no user interaction - the time screen saver activation is based upon).

    I am ok with using a osascript breakout to retrieve it, but I can't figure out
    how... Anyone can help me?

    There are probably a lot of ways to do that. In Panther I have been using a simple shell script to get the CPU idle time from 'top' (note: top does not work the same in Jaguar). This works for me. There maybe (and probably are) much better ways to do this.

    # get CPU 3 second idle time
    X=`top -l 2 -s 3 0 | grep idle | sed 's/^.*\\(......idle\\)/\\1/ s/\..*$//' | tail -1`
    echo "Average CPU idle time for the last 3 seconds has been ${X}% idle"

    -Ernie-

    "There are only two kinds of computer users -- those who have suffered a catastrophic hard drive failure, and those who will."

    Have you done your backup today?

  3. But surely there is an api at a higher level I could get the info from? What triggers the screen saver? Even iChatr will tell me how long I've been idle, how does it know?

    There must be a way...

    Sandman[.net]

  4. ~> ioreg -n IOPower -l |grep idle

    The above took about seven seconds to execute, and those aren't valid unix timestamps, so I don't know what to do with them....

    Sandman[.net]

  5. You sure it wasn't more like 9-10 seconds? Because those numbers sure look like ns to me. Nine billion or so ns works out to nine or so seconds.

    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/

  6. Since MacOS X is only relatively idle (ie. the null process runs with most of the CPU(s)), I'd think of another way to determine this. There are system functions that get this information from the Mach kernel. Check the sources to top or 'w' to see what they use.

    DeeDee, don't press that button! DeeDee! NO! Dee...

  7. man ioreg

    I think you'll get what you want by starting with "ioreg -n IOPower -l". Look for keys named HIDIdle-- there's more than one, so look closely to get what you actually want.

    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/

  8. Previously, Tom Harrington wrote:

    man ioreg

    I think you'll get what you want by starting with "ioreg -n IOPower -l". Look for keys named HIDIdle-- there's more than one, so look closely to get what you actually want.

    Oh my goodness! I admit that I misread what the OP was asking. But, prey tell, from what depth (insight) did that gem come from? It appears to be exactly what the OP was asking. I'm impressed (bowing down).

    -Ernie-

    "There are only two kinds of computer users -- those who have suffered a catastrophic hard drive failure, and those who will."

    Have you done your backup today?

  9. Thanks, but that's not the kind of idle time I am looking for...

    Sandman[.net]

  10. ~> ioreg -n IOPower -l | grep idle

    These were all executed pretty much after each other, and neither took more than three seconds to complete. The 43379648282 line was definately not 43 seconds after I woke it, at least I'm pretty sure.

    Sandman[.net]

  11. All the others are clearly labelled as keyboards, pointing devices, etc, if you look at the tree. If all of these idle times were measured simultaneously then the last one would probably be the minimum of the others, but using this command it gets measured some ns later.

    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/

  12. I don't have any documentation to back up the idea that ioreg returns ns in thie case. But the lookup follows the same general scheme I've followed in some Objective-C code where I use IOKit calls to get system idle time-- where I do get ns.

    Using ioreg even tracks an undocumented change in reporting time between Jaguar to Panther. The IOKit calls on Jaguar and earlier return an NSData, while on Panther they return an NSNumber. Trying the same command on Jaguar shows different results that match this change.

    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/

  13. Previously, Tom Harrington wrote:

    I don't have any documentation to back up the idea that ioreg returns ns in thie case. But the lookup follows the same general scheme I've followed in some Objective-C code where I use IOKit calls to get system idle time-- where I do get ns.

    See:

    http://macosxlabs.org/tools_and_scripts/script_archive/scripts/idle/stanf ord_university/idler.html

    Confirms that you get seconds by dividing by 10^9.

    Regards

    Martin

  14. That's not exactly an official declaration, but at least I'm not the only one who reached this conclusion. :-)

    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/

  15. Hmm, this seems to do the trick! Thanks a bunch!

    Sandman[.net]

  16. ...but which idle-time are you using here? It seems to be correct, but I would like to know. The above reports four different idle times

    Sandman[.net]

  17. They're certainly in some sort of fractional second. The next question is which idle time is the correct one. I'm assuming the bottom one is -- the IOHIDSystem object which is a child of the IOPlatformExpertDevice, which is a child of the root device.

    Matthew T. Russotto mrussotto@speakeasy.net "Extremism in defense of liberty is no vice, and moderation in pursuit of justice is no virtue." But extreme restriction of liberty in pursuit of a modicum of security is a very expensive vice.

  18. sleep 60
    ioreg -n IOPower -l | grep HIDIdleTime

    -Ernie-

    "There are only two kinds of computer users -- those who have suffered a catastrophic hard drive failure, and those who will."

    Have you done your backup today?

  19. Sandman wrote:

    Yes, but if you look above, the ioreg reports four different HIDIdleTime - which one is the correct one?

    I took the last one, which I think is the "overall" number. I really don't know the details of how each is reset, but I would think that taking one of the device-specific numbers wouldn't be what we're looking for.

    Jeremy | jeremy@exit109.com

  20. This will execute much faster:

    ioreg -c IOHIDSystem

    Get the idle time in seconds with something like:

    ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,"\n"; last}'

    Jeremy | jeremy@exit109.com

  21. Sandman wrote:

    ...but which idle-time are you using here? It seems to be correct, but I would like to know. The above reports four different idle times

    Should be system idle, same as for the screen saver and iChat.

    Jeremy | jeremy@exit109.com

  22. 4:
    +-o IOResources <class IOResources, registered, matched, active, busy 0, r$ +-o IOHIDSystem <class IOHIDSystem, registered, matched, active, busy 0,$

    Well, first the first one seems to register keypresses, the second mouse movement and the third mouse buttons.

    One one of my machines, I have seven different idle times, and all seem to be resetted when I move my mouse. On my powerbook, all are affected when I move my mouse but not all are more or less equal in value...

    Sandman[.net]

  23. I agree. Just wanted to hear what you were thinking. Thanks a alot.

    Sandman[.net]

  24. The other day, while searching the Apple site, looking for some AppleScript information, I came upon a link to macosxlabs.org which had a link to some C-source code at Stanford University to a program called 'idler.c':

    http://macosxlabs.org/tools_and_scripts/script_archive/scripts/idle/stanf ord_university/idler.html

    <quote>
    /*****************************************
    * idler.c
    *
    * Uses IOKit to figure out the idle time of the system. The idle time * is stored as a property of the IOHIDSystem class; the name is * HIDIdleTime. Stored as a 64-bit int, measured in ns.

    <end quote>

    You can download either the source or a binary from that site.

    -Ernie-

    "There are only two kinds of computer users -- those who have suffered a catastrophic hard drive failure, and those who will."

    Have you done your backup today?

Power Manager - automation and energy saving software for Mac OS X

Download Power Manager and start saving.