System idle time - how to retrieve?

I want to know how long, preferably in seconds, the system has been idle
Sandman wrote on :

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?

Ernie Klein replied on :

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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"

“Michael Vilain replied on :

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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.

Sandman replied on :

In article vilain-8CA285.13060506042004@redacted.invalid, "Michael Vilain vilain@redacted.invalid" wrote:

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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...

Tom Harrington replied on :

In article mr-53FB2C.22182906042004@redacted.invalid, Sandman mr@redacted.invalid wrote:

In article vilain-8CA285.13060506042004@redacted.invalid, "Michael Vilain vilain@redacted.invalid" wrote:

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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.

Sandman replied on :

In article eckleinspammenot-FCA579.13034106042004@redacted.invalid, Ernie Klein eckleinspammenot@redacted.invalid wrote:

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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 replied on :

In article tph-C0B252.15225706042004@redacted.invalid, Tom Harrington tph@redacted.invalid wrote:

In article mr-53FB2C.22182906042004@redacted.invalid, Sandman mr@redacted.invalid wrote:

In article vilain-8CA285.13060506042004@redacted.invalid, "Michael Vilain vilain@redacted.invalid" wrote:

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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....

Tom Harrington replied on :

In article mr-403C73.00241707042004@redacted.invalid, Sandman mr@redacted.invalid 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.

Ernie Klein replied on :

In article tph-C0B252.15225706042004@redacted.invalid, Tom Harrington tph@redacted.invalid 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).

Sandman replied on :

In article tph-682438.17502906042004@redacted.invalid, Tom Harrington tph@redacted.invalid wrote:

In article mr-403C73.00241707042004@redacted.invalid, Sandman mr@redacted.invalid 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.

Matthew Russotto replied on :

In article tph-682438.17502906042004@redacted.invalid, Tom Harrington tph@redacted.invalid wrote:

In article mr-403C73.00241707042004@redacted.invalid, Sandman mr@redacted.invalid 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.

Ernie Klein replied on :

In article 9p6dnZ2KMs2jhendRWPC-g@redacted.invalid, russotto@redacted.invalid (Matthew Russotto) wrote:

In article tph-682438.17502906042004@redacted.invalid, Tom Harrington tph@redacted.invalid wrote:

In article mr-403C73.00241707042004@redacted.invalid, Sandman mr@redacted.invalid 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

Tom Harrington replied on :

In article 9p6dnZ2KMs2jhendRWPC-g@redacted.invalid, russotto@redacted.invalid (Matthew Russotto) wrote:

In article tph-682438.17502906042004@redacted.invalid, Tom Harrington tph@redacted.invalid wrote:

In article mr-403C73.00241707042004@redacted.invalid, Sandman mr@redacted.invalid 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 Harrington replied on :

In article mr-4EE2A2.10495507042004@redacted.invalid, Sandman mr@redacted.invalid wrote:

In article tph-682438.17502906042004@redacted.invalid, Tom Harrington tph@redacted.invalid wrote:

In article mr-403C73.00241707042004@redacted.invalid, Sandman mr@redacted.invalid 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.

Martin replied on :

In article tph-28AED8.10342607042004@redacted.invalid, Tom Harrington tph@redacted.invalid 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

Tom Harrington replied on :

In article c51euu$2tg$1$8300dec7@redacted.invalid, Martin bitbucket@redacted.invalid wrote:

In article tph-28AED8.10342607042004@redacted.invalid, Tom Harrington tph@redacted.invalid 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. :-)

Jeremy Nixon replied on :

Sandman mr@redacted.invalid 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 @redacted.invalid)/1000000000; print $idle,"\n"; last}'

Sandman replied on :

In article 1078njmiva25g3c@redacted.invalid, Jeremy Nixon jeremy@redacted.invalid wrote:

Sandman mr@redacted.invalid 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 @redacted.invalid)/1000000000; print $idle,"\n"; last}'

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

Sandman replied on :

In article 1078njmiva25g3c@redacted.invalid, Jeremy Nixon jeremy@redacted.invalid wrote:

Sandman mr@redacted.invalid 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 @redacted.invalid)/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

Jeremy Nixon replied on :

Sandman mr@redacted.invalid 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.

Sandman replied on :

In article 1078t9qa8iaus98@redacted.invalid, Jeremy Nixon jeremy@redacted.invalid 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...

Jeremy Nixon replied on :

Sandman mr@redacted.invalid 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.

Sandman replied on :

In article 10792ind7d2j2b0@redacted.invalid, Jeremy Nixon jeremy@redacted.invalid wrote:

Sandman mr@redacted.invalid 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.

Ernie Klein replied on :

In article mr-FCBC16.21260606042004@redacted.invalid, Sandman mr@redacted.invalid 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

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