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.

Trying to find out if a particular user is logged into the machine, specifically the user using the graphical user interface.

Is this possible via command line?

share|improve this question
1  
Wait GUI and command line? Both? Or just command line? I'm confused. –  Vervious Aug 26 '10 at 2:46
    
@Nano8Blazex, made the question more clear for you. –  Gerald Kaszuba Aug 26 '10 at 3:10
    
:D that's great! –  Vervious Aug 26 '10 at 4:29

2 Answers 2

up vote 8 down vote accepted

GUI:

  • Open the Accounts preference pane in System Preferences. The pre-selected user account will be the active user account.
  • If fast user switching is active its menu extra (the menu on the right side of the menu bar) can be configured to show the name of the active user.

Command Line:

  • Check the owner of /dev/console

    stat -f '%u %Su' /dev/console
    
  • Write a program that uses the official API (SCDynamicStoreCopyConsoleUser; see below)

In a C program:

The C code in Technical Q&A QA1133: Determining console user login status shows how to determine which user owns the active GUI session.

For example:

/* Adapted from QA1133:
 *    http://developer.apple.com/mac/library/qa/qa2001/qa1133.html
 */
#include <assert.h>
#include <SystemConfiguration/SystemConfiguration.h>

int main(int argc, char **argv) {
    SCDynamicStoreRef store;
    CFStringRef name;
    uid_t uid;
#define BUFLEN 256
    char buf[BUFLEN];
    Boolean ok;

    store = SCDynamicStoreCreate(NULL, CFSTR("GetConsoleUser"), NULL, NULL);
    assert(store != NULL);
    name = SCDynamicStoreCopyConsoleUser(store, &uid, NULL);
    CFRelease(store);

    if (name != NULL) {
        ok = CFStringGetCString(name, buf, BUFLEN, kCFStringEncodingUTF8);
        assert(ok == true);
        CFRelease(name);
    } else {
        strcpy(buf, "<none>");
    }

    printf("%d %s\n", uid, buf);

    return 0;
}
share|improve this answer
1  
Nice accepted answer steal :) –  Gerald Kaszuba Aug 28 '10 at 4:36

Via the command line, who and users should work.

share|improve this answer
    
Ah, who and look for "console". Thanks. –  Gerald Kaszuba Aug 26 '10 at 3:09
    
@Gerald: Using who is not accurate on my 10.4 system when using fast user switching (the system I am using right now shows another user on “console” even though my GUI session is the active one). Maybe it is more reliable in newer versions. –  Chris Johnsen Aug 26 '10 at 5:39

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.