Use a script and launchd task to keep AirDisks spinning
Apr 08, '08 07:30:00AM
Contributed by: finitesquid
As we all know by now, with the 7.3.1 firmware update to the AirPort Extreme (802.11n) Base Station, we finally get decent read/write performance from attached disks. The pre-7.3.1 firmware was buggy and slow, but the new firmware rocks, and AirDisks are finally ready for prime time (as well as supporting Time Machine -- yay!).
One of the energy/disk saving features of the new base station firmware is that it will spin down attached drives if they have not been accessed in approximately 10 minutes. This is good for saving a couple of watts of power, as well as prolonging the life of a hard drive. If a drive is used as a Time Machine volume, the delay in waiting for the drive to spin up and be accessible is quite acceptable.
However, if the drive is being used as a network share for data (in my case, iTunes media and archival data), the five to ten second delay caused by spinning up is not acceptable (at least to me). And since there's no parameter to disable disk sleeping on the AirPort, I wrote a trivial program and launchd plist file to "tickle" the disk every few minutes to keep it spinning (and just that one disk - I have two other USB disks attached to that basestation that are for Time Machine, and I do want them to spin down).
So, here are the main features of TickleDisk:
- A script named TickleDisk is run every five minutes by launchd
- This script only runs when you are logged in to your account on the Mac it's installed on.
- TickleDisk checks to see if it's within a time window that I want to keep the disk spinning (e.g. between 9am and midnight). If it's outside of the time window, it just exits.
- If it is in the time window to keep spinning, it checks to see if the disk is mounted. If it's not, it just exits.
- If the disk is mounted, it then touches an invisible zero-length file at the root directory of the disk. This is enough to keep the disk spinning. And my access to the data on the disk immediate.
The script below needs to be saved somewhere on your Mac, and be made executable. With your favourite editor, cut and paste the code below, and save it somewhere convienient (I have mine saved as ~/bin/TickleDisk, you may want it saved as /usr/local/bin/TickleDisk or maybe /Applications/Utilities/TickleDisk):
#!/bin/sh
dir=$1
hour=`date '+%H'`
if [ $hour -ge 1 -a $hour -le 9 ]; then
exit 0
fi
if [ -d $dir ]; then
touch "$dir/.com.timofejew.tickledisk"
fi
exit 0
After creating this file, make it executable by typing in chmod 755 ~/bin/TickleDisk (or whatever you ended up calling it) in a Terminal window. You'll notice from the script that I don't want it to "tickle" the disk between 1am and 9am (that's the 1 and 9 you see in the first if statement). Modify this time window to your needs (remember it's a 24 hour clock, from 0 to 23).
The plist file below needs to be placed in the directory ~/Library/LaunchAgents and called something like com.timofejew.TickleDiskNameDrive.plist (where DiskName is the name of the AirDisk that is meaningful to you).
There are a couple of things to modify in the plist file to get this to work:
- The <string> tag in the Label section needs to be the same as the name of the plist filename.
- The first <string> tag in the ProgramArguments section needs to be the full path to the TickleDisk script.
- The second <string> tag in the ProgramArguments section is the mounted path of the AirDisk (usually something like /Volumes/NameOfAirDisk, where NameOfAirDisk is the name of the disk that you want to keep spinning.
And, finally, you'll need to log out, log back in, and mount the disk that you want to keep spinning (if it's not mounted, then it won't be touched). BTW, my apologies to those that get a bit lost with the files and instructions here: I've been a UNIX hack for over 20 years, and I always assume at least a bit of nerdy UNIX-ish knowledge. And yes, it's not the most robust script ever coded, but as long as you pass a valid directory, it'll work just fine -- it's not meant to be called manually.
[robg adds: I haven't tested this one. Many years ago, we ran this hint which set up a cron task to tickle a disk; this hint is a modern, more robust version of the same thing, basically.]
Comments (6)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20080331184914894