Shammer's Philosophy

My private adversaria

Invoke some script when system shutdown on Mac OS X

What I would like to do

I found How can I use launchd to run logout script | Discussion | Jamf Nation, and the way posted at 10/30/15 7:05 PM by Bruienne sounds good. His idea is invoking some script as a LaunchAgent which uses trap command waiting SIGINT, SIGHUP and SIGTERM. The process launched by LaunchAgent when a user logs in wouldn't be finished unless a user send kill them intentionally or System Shutdown. If this script has a infinite loop, the end of this script is the same time when system would be shutdown or user logout. Using this way enables to invoke some scripts when system would be shutdown.

Script Skelton

onLogout() {
    # Insert whatever script you need to run at logout

echo "INFO - Watching ${HOME}" >> /var/log/


while true; do
    sleep 86400 &
    wait $!

plist file($HOME/Library/LaunchAgent/logout-watcher.plist)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">