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:


Close the lid on my Macbook, quickly.
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 ] &amp;&amp; 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)'