30

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?

  • what exactly didn't work out? – nohillside 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
30

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)

  • 1
    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 '16 at 16:17
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.

16

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.

  • 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. – nohillside 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
  • 5
    Note that « ≠ << – mlainz Jan 25 '16 at 21:46
2

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.

2

This does the trick for me.

sudo -s

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! Obligatory warning - killing things as root has no undo and no "are you sure you want to interrupt this process without saving your files, including system critical databases that might render the machine unbootable on rare occasion." type warnings.

killall -vu username -9
  • 1
    No need to into root shell - just stick sudo in front of the killall commands – Mark Oct 18 '18 at 20:10
1

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

  • I had to break the body out onto a new line and put the closing curly brace afterwards as well. – bmauter Dec 16 '18 at 3:09
  • @bmauter not for me, in the suggested form works perfectly. – Kappe Dec 17 '18 at 9:37
-3

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

[host:~user]$ exit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .