Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I want to logout my Mac OS X from the command line. My OS X version is 10.9. I tried command pkill -KILL -u uid, but this command didn't work out. Is there a command that can be used to log out a user from the system using command line only, not AppleScript?

share|improve this question
    
what exactly didn't work out? –  patrix Apr 7 at 13:27
    
on running the command pkill -KILL -u uid the system shows me a grey screen and on waiting for long the system shows the force quit window with no application running –  prateeak ojha Apr 7 at 13:29

3 Answers 3

This has worked for me in the past:

Log out (with confirmation)

osascript -e 'tell app "System Events" to log out'

Log out directly (no confirmation)

osascript -e 'tell app "System Events" to  «event aevtrlgo»'

or

osascript -e 'tell application "loginwindow" to  «event aevtrlgo»'

This way any running application will get noticed and can terminate in a safe fashion.

share|improve this answer
    
osascript -e 'tell app "System Events" to «event aevtrlgo»' showing error: a "<" cant go sfter this to . –  prateeak ojha Apr 7 at 13:34
    
Hmm, works ok for me. Can you try from a shell script instead of the command line, maybe the shell does some strange stuff here. –  patrix Apr 7 at 13:43
    
I'm guessing the OP has some other issue and the normal commands are getting hung up from the comments here and the question description. –  bmike Apr 7 at 16:49

To log out purely from terminal (or a remote ssh session), just kill the loginwindow process:

sudo pkill loginwindow

You could get fancy and specify the user if multiple users have a loginwindow process, but this is an easy one shot, no prompt way to end a user's graphical session.

share|improve this answer
    
This did work on earlier OSX versions, but has stopped working for a lot of time, IIRC at least since 10.8. –  Lohoris Nov 4 at 12:11
    
@Lohoris This still logs me out on 5 of 5 macs I've tested since your comment. One is 10.8.5, one 10.9.5 and the rest are 10.10. –  bmike Nov 4 at 17:35
    
It might count as "logged out", but if you check with ps -auxw you'll notice there are still many processes pertaining to the "logged out" user. –  Lohoris Nov 4 at 17:43
    
@Lohoris User processes are started before you log in on OS X now. Killing loginwindow does the same as the log out option on OS X graphically or using AppleScript as the OP here doesn't want. The processes that depend on the graphical log in are all children of the loginwindow processes that gets spawned by the user account that logs in. –  bmike Nov 4 at 17:53
    
Knowing this I've tried and you're right: even "logging out" normally leaves some processes running. Would you edit your post so I can reverse the downvote? Actually it might sense to integrate this information anyway. –  Lohoris Nov 4 at 20:37

If you have multiple users ....

Find the Process ID with:

ps aux|grep login

Then kill that process and you logged that session out. But there are a lot of procceses left.

Check with pstree so you know which process to terminate.

share|improve this answer

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.