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.

  • Since logging out of your account unmounts all drives, is there a significant downside to just logging out? Also, I believe the OS flushes all filesystem IO writes, so the chance of an actual problem with ejection during sleep seems quite low to me - doubly so for journaled filesystems.
    – bmike
    Jan 2 '14 at 18:10

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!

  • I could use some review on this answer to ensure that I am activating the .plist thing correctly. Dec 10 '13 at 4:27
  • Followed it step-by-step, but it doesn't work for me. After sleep I unplug my external hard-drive, my MacBook5,3 wakes up and when I open the lid I get the unproperly ejected disk error. Dec 10 '13 at 13:20
  • This works for me, but only sometimes. Not sure why. Dec 16 '13 at 18:45
  • 1
    I'm still on ML, so it's not Mavericks specific, and I don't get the errors when the machine is active. I noticed that the .sleep script sometimes gets executed at wake, as if it doesn't get enough time to do so when it's going to sleep. Dec 16 '13 at 19:05
  • 1
    Also a good guide here: damacy.net/post/9377132648/…
    – Dan
    Feb 5 '15 at 19:56

You must log in to answer this question.

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