14

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?

1

5 Answers 5

7

Logout hooks were deprecated in 10.4, but they still work as of 10.9.

sudo defaults write com.apple.loginwindow LogoutHook ~/.logouthook
echo $'#!/bin/bash\nsay a' > ~/.logouthook
chmod +x ~/.logouthook

The value of the LogoutHook key can only be a path to an executable and not a shell command. The logout hook is run as root.

The defaults command modifies /var/root/Library/Preferences/com.apple.loginwindow.plist. Adding a LogoutHook key to /Library/Preferences/com.apple.loginwindow.plist doesn't work.

If a logout hook takes long enough to run, a gray screen is shown until the logout hook terminates. There doesn't seem to be any time limit after which logout hooks are forced to terminate.

I haven't figured out any way to run programs on logout reliably with launchd. When I tried trapping signals like EXIT, the code in the trap was only run when I logged out to the login window and not when I shut down or restarted.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//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>ProgramArguments</key>
  <array>
    <string>bash</string>
    <string>-c</string>
    <string>trap 'echo a>/Users/username/Desktop/a;say a' EXIT;while :;do sleep 10;done</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>
4
  • 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! Jul 1, 2011 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! Jul 2, 2011 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 :( Jul 2, 2011 at 19:57
  • 2
    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.
    – mklement0
    Nov 6, 2012 at 22:59
4

Actually there is a non-deprecated way to do it:

  1. Open the "Script Editor" app
  2. Paste
    do shell script "PUT YOUR START SCRIPT PATH HERE"
    
    on quit
        do shell script "PUT YOUR STOP SCRIPT PATH HERE"
        continue quit
    end quit
  1. File > Save, Select Application and Stay open after run handler and Save
  2. System Preferences > Users & Groups > Login Items tab > + button
  3. Add the application and mark it as Hidden

To hide it from the Dock:

  1. Right-click the application and select Show package content
  2. Open Info.plist and add
<key>NSUIElement</key>
<string>1</string>
1
  • Love this. Simple and adequate for my needs. And not deprecated.
    – Ivan X
    Dec 20, 2019 at 20:43
0

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.

1
  • 1
    This is deprecated now Mar 8, 2018 at 5:39
0

You could also create a Mac application from your script using something like Platypus and then add it to Login Items like any other application.

0

Power Manager can run a script when a user logs out. This is a commercial product and it supports the latest macOS.

Power Manager run a script on log out Schedule Assistant task

Earlier versions required using the event editor, but a Schedule Assistant task is now included for running scripts at log in and out.

I wrote Power Manager, so feel free to ask technical questions about its implementation. In the case of log out triggered events, Power Manager does not poll for changes in user state but instead is notified of user changes by macOS. This approach is more efficient that having a script looping/polling.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .