Log out graphical user from command line

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?


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

EDIT:

sudo kill WindowServer

This will log the user out.


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).


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.


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 ;)