Shell or Applescript to delay shutdown? We have a workstation that gets used sporadically, and I'm trying to setup a script that backs it up during shutdown.
I created a LogoutHook in com.apple.loginwindow to a 1-line bash script, that triggers an Applescript that compares my own log entries to determine if the backup should run. If so, it calls the main Applescript which triggers a  Chronosync backup job.

It all works... except that I barely get the backup job started before the Mac terminates it and shuts down. I suppose SIGTERMs and SIGKILLs are being issued and all processes quickly comply.

I also looked into the "proper" way of doing it with launchd (since LogoutHooks are deprecated). Launchd doesn't really support scheduling jobs upon shutdown: it requires starting the script at login, sleeping/waiting to trap a SIGTERM. Again, this poses the problem that the entire system is shutting down within seconds, from the network stack to the Chronosync daemon.

The man page for the shutdown command also states:


  Upon shutdown, all running processes are sent a SIGTERM followed by a SIGKILL.  The SIGKILL will follow the SIGTERM by an intentionally indeterminate period of time.  Programs are expected to take only enough time to flush all dirty data and exit.




Is there a way to delay shutdown until my scripts are all done? Ideally I want the same behavior as Apple's Software Updater, which takes as long as it wants to install updates upon logout, before allowing the machine to shut down.
Why not just change your users' behavior on the shutdown process to make this a lot simpler (with minimal effort on scripting/programming the shutdown hook)?


Tell the users not to use the standard shutdown process (from the menu or through the keyboard shortcut) and instead use your backup AppleScript that's transformed into an app called something like SafeShutdown.app
Within the SafeShutdown app script, do the backup and then end it with a  

tell application "Finder"
    shut down
end tell


or with this (will prompt for password)  

do shell script "sudo shutdown -h now" with administrator privileges


or with a

tell application "System Events"
    shut down
end tell