50

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?

3
  • what exactly didn't work out?
    – nohillside
    Apr 7, 2014 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 Apr 7, 2014 at 13:29
  • 1
    Sending the KILL signal should be a last resort — it gives the process no opportunity to shut down cleanly, store its state, flush and close files and connections, reset hardware, or any other tidy-up, which can cause trouble in some cases.  (I usually find it best to send at least one of TERM, HUP, QUIT, ABRT, INT first, to give the process a chance to stop gracefully, and then send KILL only if all else has failed — which is rare in practice.)
    – gidds
    Jan 8 at 14:40

10 Answers 10

67

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)

3
  • 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. Mar 14, 2016 at 16:17
  • Do you know opposite command to bootout? I need login user programmatically.
    – Rougher
    Mar 30, 2020 at 7:20
  • This is great. My Mac gets stuck after a while when I tell it to log out, and it never logs out. The second command was very persuasive.
    – Radu C
    Apr 9, 2021 at 20:40
30

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.

1
  • 1
    Maybe things changed since 2014, but nowadays this would log out the current user as well (there is one loginwindow process for each logged in user).
    – nohillside
    Jan 7 at 9:11
19

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.

1
  • 10
    Note that « ≠ <<
    – mlainz
    Jan 25, 2016 at 21:46
2

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

3
  • I had to break the body out onto a new line and put the closing curly brace afterwards as well.
    – bmauter
    Dec 16, 2018 at 3:09
  • @bmauter not for me, in the suggested form works perfectly.
    – Kappe
    Dec 17, 2018 at 9:37
  • Created .bash_profile and added: logout() {sudo launchctl bootout user/$(id -u "$1")} I get the following error when I try to execute. -bash: logout: FIRSTNAME: numeric argument required I'm puzzled. The command works fine when used directly in the terminal, but for some reason this as a bash profile or bash script keeps spitting this error out. I wonder if it has to do with the fact that the username I am trying to test with has a space in it betwen First and Last Name? Seems like the Last name is being interpreted as an argument, yet the entry in .bash_profile has double quotes.
    – adamlogan
    Aug 16, 2021 at 21:44
2

It's just

launchctl reboot logout

(needs to be run under the logout-target user, so prefix with sudo -u …a_user…).

man launchctl:

… With the logout argument given, launchd will tear down the caller's GUI login session in a manner similar to a logout initiated from the Apple menu. The key difference is that a logout initiated through this subcommand will be much faster since it will not give apps a chance to display modal dialogs to block logout indefinitely; therefore there is data corruption risk to using this option. Only use it when you know you have no unsaved data in your running apps. …

1

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.

1

An extreme way to kick the user session out is to force a restart:

sudo shutdown -r now

This works out well if you are trying to update the system.

1
  • 3
    And you can kill the spiders in your home by burning it down, too. Shutting down will have the inherent effect of kicking off users but if your goal is just to log someone out forcing a restart is on the extreme side.
    – Allan
    Jul 22, 2020 at 15:14
1

To get some root privileges and the # prompt, To kill all processes of another user:

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

sudo killall -vu USERNAME -9
0
-3

This does the trick for me

sudo -su user

and for root

sudo -su root

-5

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 .