Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Logout from OS X via the Terminal UNIX
When I'm primarily using the keyboard, I find it a bit annoying to have to switch to the mouse for anything (even if just to logout). Normally, if you type logout in your Terminal window, your shell quits, but of course, you're still logged in to OS X. Add the following shell function to your .bashrc (or .bash_profile, or wherever else depending on your setup), and voila -- you really do log out...
logout() {
  osascript -e 'tell application "System Events" to log out'
  builtin logout
}
[robg adds: Depending on the state of your windows, you'll probably have to dismiss some dialog boxes in order to complete the logout, but this can be done using the keyboard as well. I'm not sure I'd replace the built-in logout function, as I've become quite used to using it to just logout from the Terminal. Instead, you might want to change the command's name to logout2, or something else, in order to prevent an accidental total logout.]
    •    
  • Currently 3.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[32,562 views]  

Logout from OS X via the Terminal | 14 comments | Create New Account
Click here to return to the 'Logout from OS X via the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Logout from OS X via the Terminal
Authored by: rhowell on Feb 21, '05 10:53:02AM

You can "tell application System Events to log out" by typing Shift-Option-Command-q.

Anyone know of an elegant way to log out another user via Terminal, other than killing his loginwindow, SystemUIServer, or WindowServer (I forget which one actually, and it often leaves his Dock running)?



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: bcamp1973 on Feb 21, '05 10:56:19AM

I have the same need. I frequently maintain my parents computer via SSH. They live in another state and it would be nice to be able to log them (or my brother) out of the computer at times. Currently i have to use VNC to do it and that's a pain. It would be so much easier if i could just force all users to log out...



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: vortmax on Feb 21, '05 11:38:07AM

As root, you can stop/kill all user processes using "killall -u [username]". It is a nice feature but one needs to be careful as always when stoping other's jobs.



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: IanWalker on Feb 21, '05 01:13:59PM

cmd+shift+Q quits all apps and logs out (you'll have to hit the return or enter key as well, or just wait the two minutes and it'll do it anyhow). The keyboard shortcut is listed right next to the "Log Out [username]..." item on the Apple menu. Seemed obvious to me.

---
Ian!



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: DavidRavenMoon on Feb 21, '05 07:02:34PM

I was thinking the same thing! I use Command-Shift-Q all the time to logout... I never use the mouse.

I often notice the trend on MOSXH if for complicated solutions to simple problems! Users will write complex scripts to do something already built into the system... I do enjoy their ingenuity however!

A lot of clever people here. :)


---
G4/466, 1 GB, Mac OS X 10.3.8



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: sapridyne on Feb 22, '05 09:21:44AM

Even better is Command + Option + Shift + Q -- no need to press Return/Enter.



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: billyhwang on Feb 25, '05 03:57:25PM

Also, if you push command + option + shift + Q, it will bypass the dialog asking if you want to log out and automatically log you out!



[ Reply to This | # ]
From the Terminal
Authored by: mistersquid on Feb 21, '05 11:16:20PM

You can leave a terminal session also by typing "exit".



[ Reply to This | # ]
From the Terminal
Authored by: rhowell on Feb 21, '05 11:21:51PM

or ctrl-d.



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: mkhaw on Feb 22, '05 02:42:40AM

With admin/root privileges you can always type "shutdown", with various optional flags, to kick everyone off with or without in fact shutting down the machine (and if shut down, optionally restart). Read the man page (in Unix-speak, shutdown(8)).

One respondent said "exit" will log you out of a Terminal window, to which a follow-on said "ctrl-d" works too. The difference is "exit" isn't affected by whether your shell has ignoreeof (in tcsh/csh, IGNOREEOF in bash) set, whereas ctrl-d is (although some csh implementations will log you out if you ctrl-d enough times in succession regardless of ignoreeof). Sorry, I don't know how zsh behaves in this regard.



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: floh on Feb 22, '05 10:19:21AM

There are basically two diffrenet types of shell invocation. A shell can be a login shell or it can be an interactive shell.
When you are inside a login shell, you have come there through a login - sounds straight forward, doesn't it? - and thus you have to logout to close the shell session via the logout command.
When you are inside an interactive shell, you haven't seen a login prompt anywhere immediately near the shell invocation. So you don't close the shell session with logout - you never logged in for that shell sesseion anyway. You use the exit command.
Whenerver you open a new terminal window in OS X, you are invocating your shell implicitly as an interactive shell. Logout in this circumstance just doesn't make any sense - speaking from a unix standpoint of view. So basically, logout in an interactive shell is an alias to exit. This might be confusing but is a better solution than to implement the keyword goto into the language java just so as to issue an error message.
Remember that in OS X you are logged in in a windowing environment. You have never come to the windowing environment through a console login, using startx or something like that. Your terminal application is just another child of your windowing session, which is itself a child of a graphical login session. For a little child to kill his grandfather by his own hand would be far too much cruelty for such a feeble mind, so logout won't be tampering anywhere up your tasks' heritage chain.
If you now, on the other hand, had come to the windowing environment through startx, returned to the virtual console, pressed ctrl-z, set the windowing session bg and then typed logout ... this would kill your windowing session and log you out completely, at least on this one virtual console. For once or twice in my life with Mac OS X I had hoped for virtual consoles. But what the heck ...
So basically, trying to do a complete logout via keyboard by aliasing the builtin logout command is, as interesting as it might be on first thought, on second thought trying to do it the unix way without getting to know, and understand, that is, the unix way on the first hand; on a unix operating system which does it's best to hide away it's unix heritage.
Sorry for the rumpling. Just some thought from a traveler of platforms.



[ Reply to This | # ]
Logout from OS X via the Terminal
Authored by: derekhed on Feb 22, '05 11:43:22AM

Excellent post!

Thank you.



[ Reply to This | # ]
new Terminal windows run "login shells"
Authored by: hayne on Feb 22, '05 06:22:00PM
Whenerver you open a new terminal window in OS X, you are invocating your shell implicitly as an interactive shell. Logout in this circumstance just doesn't make any sense - speaking from a unix standpoint of view.
Actually, each new Terminal window starts a "login shell". You can see this by looking at the parent process of the shell - it is 'login'. This is important to know because it means that, for example, bash does not read the ~/.bashrc file when a new Terminal window is opened, but it does read the ~/.profile file (see 'man bash').

And this is the reason why typing 'logout' in a Terminal window will close that window, while 'exit' merely quits the shell and leaves the window open. If you start a sub-shell by typing 'bash' in a Terminal window, then that sub-shell is a non-login shell and so 'logout' gives you an error message.

In contrast, a new xterm window starts a non-login shell.

[ Reply to This | # ]

new Terminal windows run "login shells"
Authored by: PeteVerdon on Mar 12, '05 04:48:07PM

> 'exit' merely quits the shell and leaves the window open

Not on my machine, it doesn't. I always use exit to close a terminal window, because I didn't log in to it and so that's what feels natural after years of Unix. logout also works, as a convenience, but I don't use it in local windows.

Pete



[ Reply to This | # ]