What Is the Mac OS X Terminal Command to Log Out the Current User?
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'"
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.
In terminal, run this:
ps -Ajc | grep loginwindow
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"
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.