Tell me more ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

We all know that Mac OS X has the very useful Login Items functionality which lets you, among other things, set up apps/scripts to run when you log in.

I'm looking for a way to setup a list of scripts/apps that run when I log out. A "Logout Items" list, if you will.
Basically, I want to write a few little cleanup scripts for myself that will run automatically when I log out or shut down.

So, I'm looking for a way to have a script (or, ideally, list of them) automatically triggered when I log out. The log out would wait for the scripts to finish (just like how the logout waits for you to click Save if an app requests it).

Is there a way to automatically trigger (a) script(s) when I log out of Mac OS X?

share|improve this question

2 Answers

up vote 4 down vote accepted

Login and logout hooks have been deprecated since 10.4, but they still work on Lion and Mountain Lion. OS X waits until the programs have terminated before logging out, so they can make logging out take longer.

Save a script like this as ~/Desktop/test and make it executable with chmod +x ~/Desktop/test:

#!/bin/bash

say {a..z}

Then add a LogoutHook key to /var/root/Library/Preferences/com.apple.loginwindow.plist with sudo defaults write com.apple.loginwindow LogoutHook ~/Desktop/test. Running programs directly or leaving out sudo don't seem to work.

Using launchd, you could keep a program running on the background and trap the EXIT signal. But it only seems to work when logging out to the login window, and not when shutting down or restarting.

~/Library/LaunchAgents/test.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>Program</key>
    <string>/Users/username/Desktop/test</string> <!-- ~/ doesn't work -->
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

Save this as ~/Desktop/test and make it exectuable with chmod +x:

#!/bin/bash

trap 'say {a..z}; exit' EXIT
while :; do sleep 1; done

The agent can be loaded with launchctl load ~/Library/LaunchAgents/test.plist or by logging out and back in once.

share|improve this answer
Hmm, neither of those has worked for me. I've got this script, which works if I run it with sh (it creates that .txt file). I saved the script as /etc/rc.shutdown.local, and I added it with defaults as you said. Neither file was there already. Am I doing something wrong? Thanks! – Nathan Greenstein Jul 1 '11 at 21:35
I'm not sure if I'm doing something wrong or not. I'm on 10.6.8 and LogoutHook isn't working. I'd appreciate if if you could take a look at this screenshot and let me know if something looks wrong. Thanks! – Nathan Greenstein Jul 2 '11 at 16:20
1  
Ah! That last one did the trick on 10.6.8. Thanks! One note though: it doesn't work with Lion :( – Nathan Greenstein Jul 2 '11 at 19:57
1  
Both methods work fine on my Mountain Lion (10.8.2) machine, even when shutting down or rebooting. Note that the LoginHook/LogoutHook hooks run in the context of the root user, and that they are single, system-wide hooks, and that the login hook runs synchronously - much earlier than per-user launch agents; similarly, the logout hook runs earlier than a per-user launch agent that uses the EXIT-trap method. There are cases where only the hooks work; for instance, if you want to mute the sound on shutdown in order to suppress the Mac's startup sound, only the LogoutHook works reliably. – mklement Nov 6 '12 at 22:59

Script Timer is a good choice for this. It can run at logout, login, and much more. I'd go with this for a simple and easy to use solution. It has a simple GUI:

Triggered action

There are two things you need to note about Script Timer. One, it is not free. It costs $12, but I personally think it's worth it. Two, it isn't fully compatible with Lion, but they have promised a free update as soon as they've made it compatible.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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