4

I've seen a variety of solutions to check how long my machine has been idle, and I know I can check how long since last reboot with uptime, but I'm curious if there's a way to see how long since my computer was last idle. Maybe since the last time the power save settings kicked in, or last time since the screensaver turned on / monitor turned off, etc?

I'm fine using an applescript or shell command to determine this, if those are the best solutions.

  • 1
    What do you mean with idle? When it went to sleep? – Gerry Jul 18 '12 at 15:00
  • @Gerry Sleep, monitor dim, screen saver, anything really. Preferably, I'd like to see "time since user last did not enter input (key or mouse) for 1 minute". – hookedonwinter Jul 18 '12 at 15:02
2

You can also find the idle time in UNIX by running

ioreg -c IOHIDSystem

And looking for the returned HIDIdleTime

Or you could run a shell script on idle like so, just call it every 30 seconds or so using LaunchD. Make sure to put the launchd xml file in /Library/LaunchDaemons so it runs even when no user is logged in, or /Library/LaunchAgents/ if you only want it to run when someone is logged into the machine.

#!/bin/bash
idl=$"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`"
idletime="600"
if [ $idl -gt $idletime ]; then
#Your
#Code
#Here
fi
1

You can use an applescript.

on idle display dialog "Hey, your computer wasn't busy so I thought I would higjack it" end idle

Save it as an application and keep it running in the background. Maybe just write a timestamp to a file to read later?

1

You can use the following script to show the idle seconds ticking up, from Inactivity and Idle Time on OS X:

#!/usr/bin/env perl
my $idle_seconds_command = 'echo $((`ioreg -c IOHIDSystem | sed -e \'/HIDIdleTime/ !{ d\' -e \'t\' -e \'}\' -e \'s/.* = //g\' -e \'q\'` / 1000000000))';
print "Counting seconds of inactivity... Command + Period (.) to quit\n\n";
do {
    my $idle_seconds = `$idle_seconds_command`;
    chomp($idle_seconds);
    print "Idle for $idle_seconds seconds.\n";
    sleep(1);
} while(1);

When run the script prints a line each second showing the current idle time counter.

Idle seconds on OS X

-2

Here is an example LaunchD XML file.

http://u.wanttogo.to/I9p4

Either place it in /Library/LaunchDaemons/ if you want it to run even when nobody is logged in or in /Library/LaunchAgents/ if you only want it to run when someone is logged into the machine.

Place your script in /Library/Scripts/

You need to make sure your LaunchD plist file is owner root, group wheel and permissions 644.

chown root /Library/LaunchDaemons/com.my-company.my-name.plist
chgrp wheel /Library/LaunchDaemons/com.my-company.my-name.plist
chown 644 /Library/LaunchDaemons/com.my-company.my-name.plist
  • 1
    You should edit your original answer instead of replying multiple times. – Gerry Jul 18 '12 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .