I finally found an easy to use program/daemon that will automatically shutdown any drive after a set amount of time has ellapsed. Spindown source and notes
Spindown is a system daemon that spins down idle disks and so saving energy and giving the disks a longer lifetime.
It works by checking how many blocks are read and written from the disks. When no blocks are read or written the disk is idle. When a disk stays idle long enough spindown uses sg_start to spin the disk down. It also works with usb disks and hotswappable disks because it doesn't watch the device name (hda, sdb, ...), but the device id. This means that it doesn't matter if the disk is swapped while the deamon is running.
If you are using ubuntu you need to run:
#!sudo aptitude install sg3-utils build-essential
to get the the program's one dependency and before you can run make on the source for spindown you'll need build-essential package installed
First you need to download the source code and compile it. Because there are no nasty dependencies (except for sg3_start), this should be no problem.
choose your download directory $ wget http://spindown.googlecode.com/files/spindown-0.2.2.tar.gz $ tar xvzf spindown-0.2.2.tar.gz $ cd spindown-0.2.2 $ make $ sudo make install
After compiling and installing the deamon you still need to configure it. Do this by editing the sample configuration file (spindown.conf.example) with your favorite editor. Once you are satisfied with the settings save the file and copy it to /etc/spindown.conf.
The configuration file consists of two different sections: the general section and several disk sections. There should only be one general section, but different disk sections are allowed.
[General] [Disk 0] [Disk 1] ... [Disk n]
The General section
The General section has only two parameters: cycle-time and idle-time. The cycle-time parameter defines the time in seconds between checks (looking up the device name for the disk id and updating the amount of block read and written), a good value is 60. The idle-time parameter defines how long a disk should be idle before it is spundown. Don't use a to small value for this setting, because spinning the disc up and down to much isn't good for it's health. A good value for idle-time is 3600. As of version 0.2 of spindown it is also possible to set this time per disk.
cycle-time = 60 # Number of seconds between two cycles
idle-time = 3600 # The time in seconds a disk has to be idle before it is spundown.
- Do not use a value that is to small (less than 3600)
The Disk sections
There are several Disk sections allowed, but they need to have a different id or name. Each disk can have several parameters, but only the id or name parameter is obligated (not both).
The id parameter must contain the id of the diks, this is not the device name (sda, sdb, hda, ...), but mostly a string like this ata-Hitachi_HTS54161-SB2404SJHD07JE. You can find the id of your disk by looking in /deb/disk/by-id
$ ls -l /dev/disk/by-id/ total 0 lrwxrwxrwx 1 root root 9 2008-03-20 10:38 ata-Hitachi_HTS54161-SB2404SJHD07JE -> ../../sda lrwxrwxrwx 1 root root 10 2008-03-20 10:38 ata-Hitachi_HTS54161-SB2404SJHD07JE-part1 -> ../../sda1 lrwxrwxrwx 1 root root 10 2008-03-20 10:38 ata-Hitachi_HTS54161-SB2404SJHD07JE-part2 -> ../../sda2 lrwxrwxrwx 1 root root 10 2008-03-20 10:38 ata-Hitachi_HTS54161-SB2404SJHD07JE-part4 -> ../../sda4 lrwxrwxrwx 1 root root 9 2008-03-20 10:38 scsi-S_SB2404SJHD07JE -> ../../sda lrwxrwxrwx 1 root root 10 2008-03-20 10:38 scsi-S_SB2404SJHD07JE-part1 -> ../../sda1 lrwxrwxrwx 1 root root 10 2008-03-20 10:38 scsi-S_SB2404SJHD07JE-part2 -> ../../sda2 lrwxrwxrwx 1 root root 10 2008-03-20 10:38 scsi-S_SB2404SJHD07JE-part4 -> ../../sda4
Lets say you'll need to know the id of sda, in this case this is ata-Hitachi_HTS54161-SB2404SJHD07JE. Be sure not to use the id's of the partitions. When there are more entries for the same disk it shouldn't matter wich one you chose. This method is really great for swappable disks like usb drives because the device name could change when you have several.
You can also also use the name parameter if you know you are never going to unplug the disk. Then you can just use a device name (sda, hdb) with the name parameter.
The spindown parameter is boolean, it can either be 1 or 0. When it is 1 this means that disk will be spundown when it has been idle long enough. When this parameter is not given it defaults to 0.
The command parameter tells the spindown script which parameters to use to spindown the disc with sg3_start. You should try to find these out by your self. For me it mostly is enough to use "--stop" or "--stop --pc=2". You can try these parameters by running sg_start yourself.
$ sg_start --stop /dev/sda
Just replace sda with the name of your disk and make sure that there is no activity on this disk (by unmounting it or something like that). You should hear the disk spinning down when the command worked. If this parameter is not defined "--stop" is used.
As of version 0.2 of spindown it is also possible to set the idle-time per disk. This time is also given in seconds.
Example of configuration file
So your disk sections may look something like this:
# Add as much disks as you want. # Only id is needed, for everything else the default values will be used. # If spindown is not specified the disk will not be spun down. # The default command is --stop. [Disk 0] id = ata-Hitachi_HTS54161-SB2404SJHD07JE spindown = 0 [Disk 1] id = usb-Sony_Storage_Media_1A06101710143-0:0 spindown = 1 command = --stop --pc=2 idle-time = 900 [Disk 2] name = sda spindown = 0
Disks that are not specified will not be spun down.