I recently purchased a Philips Smart Surge Protector (SPP3225WA/17). It’s nice because it can detect when an attached device is turned off and then turn off any other devices attached. This is especially useful when used with a computer because most peripherals are unusable when the computer is off, so there’s no sense in leaving them on.

It’s a great idea in theory, but I had problems using it with my Mac mini (OS X Leopard 10.5.4). Specifically, I want to use the built-in power scheduling (see “Energy Saver” -> “Schedule…” in System Preferences) to have the Mac turn off everything so we don’t have to remember to do so at the end of the day. Everything worked fine using the normal “Shut Down” command, but I didn’t want to have to make sure no one was doing anything every night. I wanted to use something like “Sleep”, but unfortunately, once the Mac went to sleep, it would turn off the attached external hard drives which would, in turn, wake the Mac back up. (It would also complain about not being properly ejected.) The most the Mac would stay asleep would be a couple of seconds.

But I found a solution! According to some information I found, there’s a way to change from using the default “sleep” and use something closer to what Windows users may know as “hibernate”. (It exists normally in OS X, but is only triggered by low battery.) To force it to always “hibernate” when sleeping (which is fine in the case of the Mac mini), you can run the following command at the command line:

sudo pmset -a hibernatemode 1

To reset this back to normal, run:

sudo pmset -a hibernatemode 3

Using this hibernate mode, the Mac no longer turns itself back on when the power to the external hard drives goes off. You can then set the Mac to sleep on whatever schedule works for you in System Preferences and everything else should turn off around 15 seconds after it starts going to sleep. (You’ll have to give it a little bit because it has to write the contents of memory to disk, which takes time.)

However, one problem still remains: OS X will still complain about the disks being removed improperly. To get around this, I wrote a small shell script which ejects them 5 minutes before the scheduled sleep time. You’ll need to tailor it to your setup, of course.

#!/usr/bin/env bash
# ~/bin/eject_external_drives

if [ -d "/Volumes/Time Machine" ]
  diskutil eject "/Volumes/Time Machine"

if [ -d "/Volumes/Storage" ]
  diskutil eject "/Volumes/Storage"

My crontab for this task looks like so:

55    11    *    *    *    /Users/ben/bin/eject_external_drives
50    11    *    *    *    /Users/ben/bin/eject_external_drives

(It tries twice before midnight.)

I haven’t used this extensively yet, but it seem to be working well in testing so far. Let me know if it works for you!