Automatic Spin-Down For Idle External Hard Drives
It is often desirable to have external hard drives - such as those attached by USB and Firewire - spin down into a power-saving or 'standby' mode after a certain period of inactivity.
Since these external drives cannot have their standby parameters set with 'hdparm' (which only handles IDE devices and not the pseudo-SCSI ones), some way other than the built-in method is required to make these drives spin down.
Requirements
K/Ubuntu 7.04 or later; The 'sg3-utils' package.
The Script
Save the following code as 'scsi-idle' and set the execute flag for it: Note: You must delete the space before the #!/bin/bash on line 1.
#!/bin/bash
declare -a DID
declare -a DSTATUS
declare -a DTIME
declare -a DOFF
interval=$1
sleepval=120
buspath=/sys/bus/scsi/devices
while [ true ]; do
thetime=`date +%s`
for scsidev in $( ls --format=across $buspath ); do
if devenum="$( ls ${buspath}/${scsidev} | grep 'block:' )"; then
devdir=$buspath/$scsidev/$devenum
read devsize < ${devdir}/size
read devremove < ${devdir}/removable
if [ $devremove -eq 0 ] && [ $devsize -gt 0 ]; then # device is the right type
read newstat < ${devdir}/stat
hhit=0; listlen=${#DID[@]} #; echo $listlen $scsidev
for ((i=0;i<$listlen;i++)); do
if [ ${DID[$i]} = $scsidev ]; then
if [ "${DSTATUS[$i]}" = "$newstat" ]; then
if [ $[ ${thetime} - ${DTIME[$i]} ] -ge $interval ] \
&& [ ${DOFF[$i]} -eq 0 ]; then
sg_start --pc=5 /dev/${devenum:6} # put into sleep mode
# sg_start --stop /dev/${devenum:6} # alternate method
echo stop /dev/${devenum:6}
DTIME[$i]=$thetime
DOFF[$i]=1
fi
else
DSTATUS[$i]=$newstat
DTIME[$i]=$thetime
DOFF[$i]=0
fi
hhit=1; break
fi
done
if [ $hhit -eq 0 ]; then # add new drive to watch list
DID[$listlen]=$scsidev
DSTATUS[$listlen]=$newstat
DTIME[$listlen]=`date +%s`
DOFF[$listlen]=0
fi
fi
fi
done
sleep $sleepval
doneRun it in the background like this:
$ sudo ./scsi-idle 1500 &
The above would cause external drives to spin-down after 25-27 min of inactivity. Even though 25 min (1500 sec) is specified, the internal delay loop can add a couple minutes to the reaction time.
Use:
sg_start --pc=3 /dev/sda
..for "standby" mode, which still causes the disc to spin down but allows it to automatically spin back up when accessed.
Enjoy!