Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 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 '14 at 13:29

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. – o0'. Nov 4 '14 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 '14 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. – o0'. Nov 4 '14 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 '14 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. – o0'. Nov 4 '14 at 20:37

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»'


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 '14 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 '14 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 '14 at 16:49
Note that « ≠ << – mlainz Jan 25 at 21:46

sudo launchctl bootout gui/$(id -u <username>)
sudo launchctl bootout user/$(id -u <username>)

Replace username with the target user's user name or replace the whole subshell with the user's uid. This tells launchctl to teardown the users login session (gui specifically refers to the user's temporary login session, user specifies the users background processes).

You can log yourself out without the sudo to test this.

Note that this will ONLY work on 10.10 or newer (see launchctl help for more)

share|improve this answer
I JUST reread your question and I'm sorry that this won't help you, but it will come up in google search results. – Iain Henderson Mar 14 at 16:17

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

This does the trick for me.

sudo su

To get some root privileges and the # prompt, then kill the processes.

killall -vu username -HUP

And if it´s not all gone. Nuke em!

killall -vu username -9
share|improve this answer
Use "su root" instead of "sudo su" – moonman239 Nov 11 '15 at 18:33

if you're logged with ssh to a remote computer you can logout by simply typing 'exit':

[host:~user]$ exit
share|improve this answer

Your Answer


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.