Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a perl script (this script, to be exact) that logs user activity based on idle time on a MacOS X Snow Leopard System. However, with Fast User Switching enabled, more than one user might be logged in at the same time. I want the script to see the computer as 'idle' if another user is active (i.e. using the GUI) through Fast User Switching.

There are numerous ways to figure out when a user logged in. Finding out when a user last "switched" seems more difficult. My solution so far was to look at /var/log/secure.log, where Fast User Switching events are logged:

tail -200 /var/log/secure.log | grep "User info context values set" | tail -1

However, for users running the perl script that are not Admin, secure.log will not be readable. Are there any alternative ways to find out which user is currently actively logged in, or what the last Fast User Switching event was?

Of course, the problem could be circumvented if I had a method for finding out the idle time for the user running the script only, not the total idle time of the computer I'm currently getting from ioreg. So, I'm open to suggestions there as well.

Thanks!

share|improve this question
    
What if I only trigger Fast User Switching (CGSession), but do not actually switch to another user when the login window is shown? And when using Vine Server, multiple users could be active simultaneously. In other words: if you're really only tracking a specific user, then maybe you should indeed find a way to see if that user is truly active, rather than trying to determine if someone else might be active? –  Arjan Sep 19 '10 at 19:05
    
In a wild guess I figured maybe CGSession might have some useful command line options, but no cigar, I guess: strings /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession | grep "^-" –  Arjan Sep 19 '10 at 19:18
    
I wouldn't be worried about "multiple users" through VNC sessions. –  Michael Goerz Sep 20 '10 at 1:38
    
You're right of course: I either want to find out whether a specific user is logged in and using the GUI at this moment, or get the idle time for that user only, instead of the whole system. –  Michael Goerz Sep 20 '10 at 1:40

1 Answer 1

up vote 4 down vote accepted

I don't know how to get time since switch, but you can get the currently active user by checking ownership on /dev/console. There's probably a better way in Perl, but the stat command'll do it:

my $user = `stat -f%Su /dev/console`;
share|improve this answer
    
seems to work perfectly! –  Michael Goerz Sep 20 '10 at 8:24
    
Nice! Also, you'll be getting root while the login window of Fast User Switching is shown: while true; do stat -f%Su /dev/console; sleep 2; done –  Arjan Sep 20 '10 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.