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, 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– prateeak ojhaApr 7, 2014 at 13:29
10 Answers
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)
-
1I 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.– RougherMar 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 CApr 9, 2021 at 20:40
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.
-
osascript -e 'tell app "System Events" to «event aevtrlgo»' showing error: a "<" cant go sfter this to . Apr 7, 2014 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, 2014 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, 2014 at 16:49
-
9
A nice utility to add to your Terminal is the "logout" command, to be used like:
logout UserName
Here the how to:
Edit your .bash_profile
nano ~/.bash_profile
Add this line:
logout() {sudo launchctl bootout user/$(id -u "$1")}
Save the file pressing
ctrl+x
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.– bmauterDec 16, 2018 at 3:09
-
-
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. Aug 16, 2021 at 21:44
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.
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
-
1No need to into root shell - just stick sudo in front of the killall commands– mmmmmmOct 18, 2018 at 20:10
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.
-
3And 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.– AllanJul 22, 2020 at 15:14
-
Many times I used this one, because my Mojave was swarming with processes of all the family's users who have LOGGED OUT (normally, using the UI, confirming everything and back to the login window). I really can't understand why MacOS behaves like this - and what is gained (except for vulnerabilities) by keeping logged-out users processes alive. Anyways, above answers let me cure that without the need to restart (i.e. logout myself too...) Sep 17, 2021 at 12:03
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. …
if you're logged with ssh to a remote computer you can logout by simply typing 'exit':
[host:~user]$ exit