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?

link|improve this question

feedback

2 Answers

up vote 9 down vote accepted

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'"
link|improve this answer
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. – Sacrilicious Apr 21 '11 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 Apr 22 '11 at 7:11
@Sacrilicous AS has the same syntax for sleep, so it'd be sleep 3600; tell app.... – Lri Apr 22 '11 at 11:16
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. – Grzegorz Adam Hankiewicz May 15 '11 at 22:18
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.