22

I would like to run something like "sleep 3600; logout", but the logout bash command only closes the current terminal. How do I close the full Mac OS X session?

7 Answers 7

23

The following Applescript will logout the current user:

tell application "System Events" to log out

You can wrap this up in a bash alias using the osascript command:

alias maclogout="osascript -e 'tell application \"System Events\" to log out'"

It is the same as clicking " > Log out [username]...", and will logout after a 2 minute wait

This is easily combined with the sleep command:

alias delayedlogout="sleep 3600; maclogout"

..or could be combined into a single alias:

alias delayedlogout="sleep 3600; osascript -e 'tell application \"System Events\" to log out'"
3
  • Well put. An enhancement to exactly answer the question would be to include what the 'sleep' syntax would be in applescript. Also, I understand the second line of code was pasted from terminal, but therefore it escaped the double-quotes around System Events unnecessarily. Commented Apr 21, 2011 at 18:33
  • @Sacrilicious good point about the sleep command. The escaped-quote on the "System Events" part is necessary, because of alias maclogout="..."
    – dbr
    Commented Apr 22, 2011 at 7:11
  • 2
    I've recently found that running the command in a terminal will not shutdown/close the system due to the Terminal itself preventing the logout due to bash not being the current process. This can easily be fixed doing '(sleep 1; command) &', where the sleep queues the job in the background and by the time it executes the terminal doesn't think it is busy. Commented May 15, 2011 at 22:18
18

There is no "nice" way to log the current user out from Terminal in OS X. The 'messy' way of doing it is to kill that user's loginwindow process. It will rudely kill all processes (programs) running under your username.

Doing this is a two-step process.

  1. In terminal, run this:

    ps -Ajc | grep loginwindow
    
  2. Then, run

    sudo kill <pid>
    

    Where <pid> is the first number (second column) from the output from the above command.

Use sudo kill -9 to force kill the process which I had to do to get this to work.

So for example, when if the output to the first command is:

joshhunt    41     1    41 5e15c08    0 Ss     ??    3:13.09 loginwindow

Then I would run sudo kill 41, enter my password, and then I am logged out.

This can be combined into an bash alias:

alias messylogout="ps -Ajc | grep loginwindow | grep -v grep | awk '{print \$2}' | sudo xargs kill"
1
  • Couldn't you just use sudo killall loginwindow? At least on 10.7 the loginwindow process seems to be opened again immediately though.
    – Lri
    Commented Jul 16, 2012 at 10:43
3

I know this is an old question but it helped me, the command I needed on OS X 10.8 is:

ps -Ajc | grep loginwindow | awk '{print $2}' | sudo xargs kill -9

The awk statement is different and the kill -9 ensures the login prompt is shown.

2
  • @gronostaj Do you really believe that an edit like that would be accepted? I see suggested edits like that rejected as “invalid edit (attempt to reply to or comment on the existing post)” or “radical change” all the time – and markl obviously doesn’t have sufficient rep to post comments yet. Commented Dec 28, 2013 at 21:23
  • @Scott you're right, I've been too quick posting that comment.
    – gronostaj
    Commented Dec 28, 2013 at 21:25
2

I think I have found the answer to how to Gracefully Logout of Mac OS X without the 2 minute wait.

I figured out that holding Shift, Option, and Command and pressing "q" will log out gracefully and not ask "if you want to log out".

So I coded an AppleScript through Automator to:

tell application "System Events"
     keystroke "q" using {command down, shift down, option down}
end tell
1

I would argue that the "nicest" way post OS X 10.9 might be launchctl gui/$(id -u <username>) bootout

The post OS X 10.9 documentation for launchctl is found by running launchctl help, but essentially the command above will teardown a user's temporary session. The alternative launchctl user/$(id -u <username>) bootout tears down the permanent session that runs user daemons while the user isn't logged in.

This can be tested by running launchctl gui/$(id -u) bootout, this will immediately log you out and cause the system to display the login window (with some delay).

2
  • At least under El Capitan, the correct command is sudo launchctl bootout gui/XXX where XXX is the numerical user ID (UID). Note that i) the bootout subcommand comes first, and ii) $(id) returns a lot more than the UID. Commented Dec 28, 2016 at 12:29
  • Updated to properly call id (id -u) Commented Jan 4, 2017 at 17:36
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 ;)

1
0

If you're logged in to a shell as the same user who is logged into the gui of the mac, you could issue a sudo-less command: launchctl reboot logout which does logout the user pretty effectively; it has the caveat of not allowing apps which are being quit from prompting for interaction while quitting, however it does not seem to imply that this is the same thing as killing them outright as a kill (SIG TERM) or kill -9 (SIG KILL) might.

You must log in to answer this question.

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