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.

  • I don't think that Apple's behaves as you expect. It probably logs you out, runs the update, and when it is finished, runs the shutdown command.
    – Ɱark Ƭ
    Oct 11, 2013 at 20:34

2 Answers 2


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
  • For the second option, wouldn't an admin password have to be entered, or does "with administrator privileges" bypass that?
    – Ɱark Ƭ
    Oct 11, 2013 at 20:37
  • Yes, an admin password will have to be entered. That's why that option says "(will prompt for password)".
    – M K
    Oct 11, 2013 at 20:38
  • 2
    Maybe I should read all the words.
    – Ɱark Ƭ
    Oct 11, 2013 at 20:39

Does the script exit after it triggers the Chronosync backup? If it does, try blocking the script until the backup finishes.

I just tried logging out after running these commands:

sudo defaults write com.apple.loginwindow LogoutHook ~/.logouthook
echo $'#!/bin/bash\nwhile sleep 1; do say a; done' > ~/.logouthook
chmod +x ~/.logouthook

The logout hook script was still running after about an hour, so there doesn't seem to be any time limit after which logout hooks are forced to terminate.

  • This definitely sounds feasible to me...The LoginWindow should continue to delay logout while the process initiated by the LogoutHook is running. The AppleScript that's being spawned by the bash script is likely being terminated because the bash script has exited (allowing the logout to commence)... Oct 12, 2013 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .