Although Apple recommends to use "launchd", 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:
If you want to remove the hook:
sudo defaults delete com.apple.loginwindow LogoutHook
If using "launchd", the idea would be to capture the SIGTERM signal. Have a look at: /System/Library/LaunchDaemons/com.apple.shutdown_monitor.plist
/etc/rc.shutdown.localto run it when shutting down. It sounds like that's what you're looking for. – michaelmichael Jul 05 '11 at 14:54