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.