
Regarding running a script at logout: you could try running logout stuff upon login, but other than that I don't know of a key that has the functionality of triggering a daemon/agent upon logout, however you could try to find an event that happens upon logout, that launchd can detect, and use that as trigger for your daemon/agent spec./definition. Now launchd will automatically load it up upon login and run it according to your specification/definition. plist file in one of the appropriate FILES directories if instead you want to run specifically when a certain disk-image/volume/filesystem is mounted, you can use StartOnMount.create a daemon or agent specification/definition (.System/Library/LaunchDaemons System-wide daemons provided by Apple. System/Library/LaunchAgents Per-user agents provided by Apple. Library/LaunchDaemons System-wide daemons provided by the administrator. Library/LaunchAgents Per-user agents provided by the administrator. ~/Library/LaunchAgents Per-user agents provided by the user. Upon login, launchd will scan through FILES and load the daemons and agents defined therein (via. This optional key causes the job to be started every time a filesystem Without a full machine reboot, then set this key to be true. In other words, if the job cannot be safely respawned This optional key specifies whether the job can only be run once and This key should beĪvoided, as speculative job launches have an adverse effect on system. This optional key is used to control whether your job is launched onceĪt the time the job is loaded.

plist files can have, among others, one of the following keys: #: man ist The preferred route is using apple's launchd. Note that this method is not recommended for deployment or anything, but if you're only using it like your question stated, that should be no problem. To undo all that, enter sudo defaults delete LoginHook


Keep in mind that the script runs as root and there is only one hook for login and logout respectively. I've tested this on OS X 10.6, and it should work even up to 10.8. There's also the Logout Hook counterpart: sudo defaults write LogoutHook /usr/local/bin/script2.sh Now, let's add it as a hook: sudo defaults write LoginHook /usr/local/bin/script.sh In your terminal, run: chmod +x ~/script.sh Open up a Terminal and enter: touch ~/script.sh There are several ways to run scripts at login/logout in OS X, some are more recent and only apply to 10.5 and above, some are rather deprecated, but the fastest one would be to add a Login Hook.įirst, create the script you want to run.
