The conversation on this page has been archived and is no longer active.

System idle time - how to retrieve?

Message #1 - Posted 2004/04/06 - Sandman

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]

Message #2 - Posted 2004/04/06 - Ernie Klein

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?

Message #3 - Posted 2004/04/06 - Michael Vilain

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?

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...

Message #4 - Posted 2004/04/06 - Sandman

Previously, Michael Vilain " wrote:

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?

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.

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]

Message #5 - Posted 2004/04/06 - Tom Harrington

Previously, Sandman wrote:

Previously, Michael Vilain " wrote:

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?

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.

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?

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/

Message #6 - Posted 2004/04/07 - Sandman

Previously, Ernie Klein wrote:

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"

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

Sandman[.net]

Message #7 - Posted 2004/04/07 - Sandman

Previously, Tom Harrington wrote:

Previously, Sandman wrote:

Previously, Michael Vilain " wrote:

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?

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.

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?

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.

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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]

Message #8 - Posted 2004/04/06 - Tom Harrington

Previously, Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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....

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/

Message #9 - Posted 2004/04/06 - Ernie Klein

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?

Message #10 - Posted 2004/04/07 - Sandman

Previously, Tom Harrington wrote:

Previously, Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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....

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.

Uh, yeah it could have been. So you think "10645369319" means 10.6 seconds? Ok...

I just woke my other mac from sleep and did the same thing over ssh (i.e. it wouldn't affect the timer)

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 18076518140

~> ioreg -n IOPower -l | grep idle

"HIDIdleTime" = 20833185660

~> ioreg -n IOPower -l | grep idle

"HIDIdleTime" = 29243511133

~> ioreg -n IOPower -l | grep idle

"HIDIdleTime" = 38559405750

~> ioreg -n IOPower -l | grep idle

"HIDIdleTime" = 43379648282

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]

Message #11 - Posted 2004/04/07 - Matthew Russotto

Previously, Tom Harrington wrote:

Previously, Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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....

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.

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.

Message #12 - Posted 2004/04/07 - Ernie Klein

Previously, Matthew Russotto wrote:

Previously, Tom Harrington wrote:

Previously, Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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....

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.

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.

I tried this - it seems to track, including mouse action seems to restart the count(s):

sleep 0
ioreg -n IOPower -l | grep HIDIdleTime

"HIDIdleTime" = 1693002419 [1.69 seconds] "HIDIdleTime" = 1712215803

"HIDIdleTime" = 1733919841

"HIDIdleTime" = 1783772146

"HIDIdleTime" = 1959137947

"HIDIdleTime" = 1970753374

"HIDIdleTime" = 1979380058

sleep 1
ioreg -n IOPower -l | grep HIDIdleTime

"HIDIdleTime" = 2672751540 [2.67 seconds?] "HIDIdleTime" = 2695931738

"HIDIdleTime" = 2718477616

"HIDIdleTime" = 2768586832

"HIDIdleTime" = 2950069294

"HIDIdleTime" = 2957440727

"HIDIdleTime" = 2963500128

sleep 5
ioreg -n IOPower -l | grep HIDIdleTime

"HIDIdleTime" = 5966984948 [5.96 seconds?] "HIDIdleTime" = 5988508013

"HIDIdleTime" = 6012830442

"HIDIdleTime" = 6062831951

"HIDIdleTime" = 6236510681

"HIDIdleTime" = 6244061556

"HIDIdleTime" = 6249133336

sleep 60
ioreg -n IOPower -l | grep HIDIdleTime

"HIDIdleTime" = 61640138220 [61.64 seconds?] "HIDIdleTime" = 61661260845

"HIDIdleTime" = 61683192128

"HIDIdleTime" = 61741556563

"HIDIdleTime" = 61938237282

"HIDIdleTime" = 61945622407

sleep 60
ioreg -n IOPower -l | grep HIDIdleTime

"HIDIdleTime" = 31041378818 [31.04 seconds? - "HIDIdleTime" = 31063896560 same as above

"HIDIdleTime" = 31085038312 but I moved the

"HIDIdleTime" = 31136684853 mouse after

"HIDIdleTime" = 31332539858 30 seconds]

"HIDIdleTime" = 31340015514

"HIDIdleTime" = 31344122345

"HIDIdleTime" = 61950855733

-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?

Message #13 - Posted 2004/04/07 - Tom Harrington

Previously, Matthew Russotto wrote:

Previously, Tom Harrington wrote:

Previously, Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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....

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.

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.

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/

Message #14 - Posted 2004/04/07 - Tom Harrington

Previously, Sandman wrote:

Previously, Tom Harrington wrote:

Previously, Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

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....

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.

Uh, yeah it could have been. So you think "10645369319" means 10.6 seconds? Ok...

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/

Message #15 - Posted 2004/04/07 - Martin

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

Message #16 - Posted 2004/04/07 - Tom Harrington

Previously, Martin wrote:

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.

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/

Message #17 - Posted 2004/04/07 - Jeremy Nixon

Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

The above took about seven seconds to execute,

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

Message #18 - Posted 2004/04/07 - Sandman

Previously, Jeremy Nixon wrote:

Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

The above took about seven seconds to execute,

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}'

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

Sandman[.net]

Message #19 - Posted 2004/04/07 - Sandman

Previously, Jeremy Nixon wrote:

Sandman wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

The above took about seven seconds to execute,

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}'

...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]

Message #20 - Posted 2004/04/07 - Jeremy Nixon

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

Message #21 - Posted 2004/04/08 - Sandman

Previously, Jeremy Nixon wrote:

~> ioreg -n IOPower -l |grep idle

"HIDIdleTime" = 9068114479

"HIDIdleTime" = 9952253768

"HIDIdleTime" = 9961995000

"HIDIdleTime" = 10645369319

...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.

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

1:

+-o AppleADBKeyboard <class AppleADBKeyboard, registered, $ +-o IOHIDSystem <class IOHIDSystem, registered, matched,$

{
"HIDIdleTime" = 152959376

2:

+-o AppleADBMouseType4 <class AppleADBMouseType4, register$ +-o IOHIDPointingDevice <class IOHIDPointingDevice, regi$

+-o IOHIDLibUserClientIniter <class IOHIDLibUserClient$

+-o IOHIDSystem <class IOHIDSystem, registered, matched,$

{
"HIDIdleTime" = 465443930

3:

+-o AppleADBButtons <class AppleADBButtons, registered, ma$ +-o IOHIDSystem <class IOHIDSystem, registered, matched,$

{
"HIDIdleTime" = 162430554

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

{
"HIDIdleTime" = 144026863

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]

Message #22 - Posted 2004/04/07 - Jeremy Nixon

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

Message #23 - Posted 2004/04/08 - Sandman

Previously, Jeremy Nixon wrote:

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.

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

Sandman[.net]

Message #24 - Posted 2004/04/17 - Ernie Klein

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?

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?

Need Help? Have a Question?

Looking for more help, comments, and answers?

Ask your questions on Ask Different. Ask Different is a community of Apple users ready to help.