Can I configure my Macbook to unmount all external drives when I close the lid?

I have a new 13" Macbook Pro running Mavericks. This computer is connected to an external USB drive which I use for my Time Machine backups and for external storage Time Machine backups occur a few times per day and the drive is largely inactive for most of the day.

When I am rushing to a meeting, to the server room or I'm heading home at the end of the day, I would like to do the following:

  1. Close the lid on my Macbook, quickly.
  2. Have the Mac automatically unmount all external drives, as quickly as possible, and forcibly if required. I'm willing to wait a few seconds for the all of lights to turn off.

Is there a way to get the computer to automatically unmount all external drives as soon as I close the lid on my Mac?

Currently if I forget to unmount the external drives, my Mac chastises me later with the error "The disk was not ejected properly. If possible, always eject a disk before unplugging it or turning it off."

I know it is best practice to always unmount a disk cleanly before removing the disk interface, but I'm looking for an automated solution. I am not very concerned if one Time Machine backup image is interrupted or corrupted, as one of the next subsequent backups will work and I very rarely ever need to restore from backup. Since the filesystem is a journaling filesystem, the journal transparently takes care of many other filesystem corruption errors.


There are a number of apps that can run scripts on sleep:

  • SleepWatcher — simple yet effective.
  • Scenario — can also run scripts at other times such as after wake.
  • ControlPlane — more customisable, lets you run scripts after many different events.

A bash script such as the following will unmount all specified disks:

VOLUMES="/Volumes/drive1 /Volumes/drive2 /Volumes/drive3"
for volume in $VOLUMES ; do [ -d $volume ] && umount -f $volume done

To unmount all volumes except /:

umount -A

or more cleanly…

osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'

Another option is Jettison (available through the App Store) - that's what I've been using for a couple of months now and haven't had any problems at all. It recently gained the option of automatically remounting still-connected disks when you wake from sleep.

The newer v1.3.0 (direct download only - I assume it's on its way to the App Store) costs more and they explain why this is in their FAQ.

If I read that correctly, one could buy the App Store version for two dollars and then upgrade to the direct-download version for free, but I haven't confirmed or tried this.

Not exactly expensive at either price, but not free like the SleepWatcher option seems to be.


I'll elaborate on @grgarside's answer.

I'm using SleepWatcher

  1. Install from ports:

    sudo port install sleepwatcher
    
  2. Read the manual at /opt/local/share/doc/sleepwatcher/ReadMe.rtf and via man sleepwatcher. Read this old Machint from Macworld: Cure an insomniac Mac with SleepWatcher System. According to the Machint I can simply add code to ~/.sleep and ~/.wakeup , or to the global files /etc/rc.sleep & /etc/rc.wakeup.

  3. Place the following code in ~/.sleep:

    osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'
    
  4. Activate sleepwatcher. Change <key>Disabled</key><true/> to <key>Disabled</key><false/>.

    sudo vim /Library/LaunchDaemons/org.macports.sleepwatcher.plist
    sudo launchctl load /Library/LaunchDaemons/org.macports.sleepwatcher.plist
    sudo launchctl list | grep sleepwatcher
    
  5. Verify:

    ps aux |grep [s]leepwatcher
    root              421   0.0  0.0  2459568   1012   ??  Ss    2:45PM   0:00.00 /opt/local/bin/daemondo --label=sleepwatcher --start-cmd /opt/local/sbin/sleepwatcher --verbose --sleep /opt/local/etc/rc.sleep --wakeup /opt/local/etc/rc.wakeup ; --pid=exec
    root              422   0.0  0.0  2460656   1888   ??  S     2:45PM   0:00.17 /opt/local/sbin/sleepwatcher --verbose --sleep /opt/local/etc/rc.sleep --wakeup /opt/local/etc/rc.wakeup
    
  6. Done!