How can I make Mac OS X run a program before shutting down?
Although Apple recommends to use &quot;launchd&quot;, the easiest is to create a shell script using, for example, Terminal's pico:
pico /Users/Shared/logoutHook.sh

The file can contain something like:
#!/bin/bash
say 'Hasta la vista baby!'

Then give execution rights:
sudo chmod +x /Users/Shared/logoutHook.sh

Finally, hook the script to the logout procedure:
sudo defaults write com.apple.loginwindow LogoutHook /Users/Shared/logoutHook.sh


If you start another application, as I haven't tested this, I think it might be useful to warn myself and you that:

We might need to kill the shutdown signal to execute what we want;

Then send a new one (beware not to kill the signal sent by our own
script)…



If you want to remove the hook:
sudo defaults delete com.apple.loginwindow LogoutHook


If using &quot;launchd&quot;, the idea would be to capture the SIGTERM signal. Have a look at: /System/Library/LaunchDaemons/com.apple.shutdown_monitor.plist