Just curious mostly.

Say I have someone logged into their Mac in the normal way -- ie. a graphical logon -- and the system becomes mostly unresponsive and I need to log them out.

I can reboot the system easily enough (and if things lock up it may be warranted) using 'sudo reboot' or the 'shutdown' command, but, is it possible to simply log them out remotely, as can be done through Apple Remote Desktop?

Edit: Is there a nice way to do it, that lets them save files or possibly even cancel the logout?

9 Answers 9


Any user with admin privileges can log them out using launchctl if it is a post 10.9 system.

sudo launchctl bootout gui/$(id -u <username>) will teardown the user's temporary session and return the system to the login window.

You can see launchctl help for more information.

Remember that a user's temporary domain (specified by gui/501 or some other user id) is defined by that user's id and the system will not map a username to the userid (but id will).


If you enable the ssh sever (remote login in sharing preferences dialog) you will be able to ssh to the Mac from another machine and issue any command you want.

For example: shutdown -r now


sudo kill WindowServer

This will log the user out.

  • Presuming I've 'ssh'ed into the box -- can I log them out without rebooting? May 20, 2009 at 15:41
  • 2
    sudo kill WindowServer This will log the user out. May 20, 2009 at 16:00

You could do a sort of:

killall -u <username>

But that is really dirty. Or just kill their logon process. This would pretty much be like having a windows box blue screen on you though. They would lose any unsaved work, etc.

I can't really think of any other way to do it more gracefully though.

  • 2
    May have to be prefixed with "sudo"
    – Brent
    May 20, 2009 at 15:43

A nice utility to add to your Terminal is the "logout" command, to be used like:

logout UserName

Here the how to:

  1. Edit your .bash_profile

    nano ~/.bash_profile

  2. Add this line:

    logout() {sudo launchctl bootout user/$(id -u "$1")}

  3. Save the file pressing ctrl+x

  4. Restart the terminal

You are ready to go ;)

  • 1
    That is nifty. (Almost wish I was still a mac admin and had a need to test it). Oct 22, 2018 at 16:33
  • @ClintonBlackmore I use it every day to log out my GF from my mac and save CPU/battery :D
    – Kappe
    Oct 23, 2018 at 9:37
  • Oh, that's funny. Oct 23, 2018 at 16:11

This almost works:

echo 'tell application "System Events" ^ log out ^ keystroke return ^ end tell' | tr '^' '\n' | osascript

If you don't mind waiting for the login window timer to countdown, this is easier:

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

Now, while you can log yourself out this way (from an SSH session), you'll get:

_RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL.

If you try to do it to someone else (even with sudo).


I'm not sure of a way to kill the user's login and allow them to possibly cancel that action or save file, though most apps under Lion will autosave. I've been having an issue where the screensaver under Lion hangs (started when I upgraded to OSX 10.7.4 - see https://discussions.apple.com/message/18398072) and the way I deal with it is as follows.

SSH into the Mac in question (this of course assumes you can do this) and

ps ax | grep [W]indowServer

this will give you the process ID of the WindowServer. Then

sudo kill -HUP *{the PID from the previous command}*

The Mac will wait a few moments while it does what it needs to do, then return you back to the Login screen.

  • This is what I needed :)
    – JoshP
    Jul 11, 2012 at 14:15

Find their WindowServer or login process:

grep WindowServer

Find the pid of the user's process

sudo kill -9

This assumes you're an admin on their computer.


Killing the Window server will take all it's child processes with it.

If you can log in as the user via SSH then you can use:

$ alias maclogout="osascript -e 'tell application \"System Events\" to log out' "
$ (sleep 1; maclogout) &

Expect to wait almost two minutes before the login window appears. In my testing the logout dialog wouldn't appear until ten or fifteen seconds after the command was given, then there's a sixty second wait and then there's the time it takes to log out.


Another alternative is the approach given in the answer by user bmike to a question in Apple.StackExchange.com:

sudo pkill loginwindow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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