Logout user via terminal

Logout user via terminal

16/07/12 | by admin [mail] | Categories: Mac OS X

Yes, sometimes you need to log people out (tidily), and sometimes you don't have access to their screen. This is a one liner that does it.

/bin/launchctl bsexec $(ps -xawwo pid,command | grep "/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder$" | awk '{print $1}') /usr/bin/osascript -e 'tell application "System Events" to log out';

Taking it apart, we are using bsexec option of launchctl to execute the osascript in the same "Mach bootstrap namespace hierarchy" as Finder. The whole middle bit (the bit in $(...)) is just doing a ps, grepping out the Finder process, and then awking the pid for the Finder process. What if there are multiple users with Finder processes? Then it breaks. But really, how often does that happen.

Please don't ask me what a "Mach bootstrap namespace hierarchy" is. The basic idea, is that if you just try to the osascript to log out, the osascript won't reach "System Events" (which belongs to Finder), and it doesn't work.

Now, the quick and dirty way to do this is to kill the loginwindow process. But that doesn't prompt users to save work, or close out apps properly or any of the other house keeping that actually Logging Out is meant to do.

Also, using AppleScript (via the terminal's open scripting architecture support) to quit Applications is handy:

osascript -e 'tell application "Microsoft Outlook" to quit';
osascript -e 'tell application "FireFox" to quit';

Trackback address for this post:

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

Comments, Trackbacks, Pingbacks:

No Comments/Trackbacks/Pingbacks for this post yet...

Comments are not allowed from anonymous visitors.

System Administration

September 2017
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  


XML Feeds

What is RSS?

powered by b2evolution free blog software