# Check for idle disks and spin them down
# run this from cron every 30 mins
# */30 * * * * /usr/local/bin/spindown.sh
# -- craiger, parts taken from Zack Reed
# space separated list of disk wwn names to monitor.
# These should be found in /dev/disk/by-id:
# Create a file on the ramdisk and cycle it to test for disk activity
( if [ ! -f /dev/shm/1 ] ; then touch /dev/shm/1 /dev/shm/2; fi ; mv /dev/shm/1 /dev/shm/2; cat /proc/diskstats > /dev/shm/1 ) >/dev/null 2>&1
# convert disk wwn id's into sdX names because kernel developers
# think they're so clever and randomize sd* names at boot, while most
# userspace tools assume they're static, including /proc/diskstats
for id in $DISKNAMES
SDLIST="$SDLIST `readlink /dev/disk/by-id/$id | cut -d/ -f3`"
# Loop through all disks and spin them down if idle.
for disk in $SDLIST
# Check if disk exists
if [ -e /dev/$disk ]; then
# Check if disk is currently spinning
if [ "$(hdparm -C /dev/$disk | grep state)" = " drive state is: active/idle" ]; then
#Check if disk has been accessed since last time
if [ "$(diff /dev/shm/1 /dev/shm/2 | grep $disk )" = "" ]; then
echo "Drive $disk is spun up and idle. Spinning down now."
hdparm -y /dev/$disk
echo "Drive $disk is spun up and being accessed. No change."
echo "Drive $disk is already spun down. No change."
echo "Drive $disk not found." #will never happen