Way to logout a user from the command line in OS X 10.9

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?


sudo launchctl bootout gui/$(id -u <username>)
or
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 macOS 10.11.x or newer (see launchctl help for more)


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.


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.