#!/bin/bash
if [ ! -f /dev/shm/1 ]
then
touch /dev/shm/1 /dev/shm/2
echo "$(date '+%F %T') Tempfile does not exist, creating" >> /var/log/hd_spindown.log
fi
mv /dev/shm/1 /dev/shm/2
sleep 12
cat /proc/diskstats > /dev/shm/1

HDB="sdb"
HDC="sdc"
export HD="sdb\|sdc"

if [ "$(diff /dev/shm/1 /dev/shm/2 | grep $HD )" = "" ]
then
if [ "$(/sbin/hdparm -C /dev/$HDB | grep "drive state" | cut -d: -f2 | awk '{ print $1}')" = "standby" ] && [ "$(/sbin/hdparm -C /dev/$HDC | grep "drive state" | cut -d: -f2 | awk '{ print $1}')" = "standby" ]
then
echo "$(date '+%F %T') $HD already spun down" >> /var/log/hd_spindown.log
else
/sbin/hdparm -y /dev/$HDB &
/sbin/hdparm -y /dev/$HDC 
echo "$(date '+%F %T') Spindown $HD" >> /var/log/hd_spindown.log
fi
else
echo "$(date '+%F %T') $HD is in use" >> /var/log/hd_spindown.log
fi
