Skip to content

Instantly share code, notes, and snippets.

@joel-wright
Last active August 29, 2015 14:02
Show Gist options
  • Save joel-wright/b99ed6eab2b92d42fff0 to your computer and use it in GitHub Desktop.
Save joel-wright/b99ed6eab2b92d42fff0 to your computer and use it in GitHub Desktop.
HD Idle Manual Spin Down
#!/bin/bash
# This script looks for recent disk access, and if nothing has changed, puts /dev/disk/by-id/${drive} into spindown mode.
#
# Array of problem drives this script looks at, listed by id (from /dev/disk/by-id/):
drives=(
"ata-WDC_WD15EARS-<ID>"
"ata-<OTHER_DRIVE>-<ID>"
)
current=`date`
DEBUG="false"
for device_id in ${drives[*]}
do
device=`readlink -nf /dev/disk/by-id/${device_id}`
d=`basename ${device}`
filename="/tmp/diskaccess-${d}.status"
stat_new=`cat /sys/block/${d}/stat | tr -dc "[:digit:]"`
if [ -f "${filename}" ]; then
stat_old=`cat ${filename} | tr -dc "[:digit:]"`
if [ "${stat_old}" == "${stat_new}" ]; then
if [ ${DEBUG} == "true" ]; then
echo ${current} "- Executing: hdparm -C /dev/${d} | grep -c 'drive state is: standby'" >> /tmp/diskaccess.log
hdparm=`hdparm -C /dev/${d}`
echo ${current} "- ${hdparm}" >> /tmp/diskaccess.log
fi
match=`hdparm -C /dev/${d} | grep -c "drive state is: standby"`
if [ "${match}" == "1" ]; then
echo ${current} "- Drive /dev/${d} already in standby; leaving current state" >> /tmp/diskaccess.log
else
echo ${current} "- Drive /dev/${d} hasn't been used; spinning down" >> /tmp/diskaccess.log
hdparm -y /dev/${d} > /dev/null
fi
else
echo ${current} "- Drive /dev/${d} has been used..." >> /tmp/diskaccess.log
echo ${stat_new} > ${filename}
fi
else
echo ${current} "- ${filename} file does not exist; creating it now." >> /tmp/diskaccess.log
echo ${stat_new} > ${filename}
fi
done

My WD Green WD15EARS refuses to spin down using the hdparm spindown timer, however it will spin down manually using hdparm -y /dev/sdX.

The following script run via cron uses /sys disk stats and a status file to check if the disk has been used and force the spindown.

This is an update to a script found here: http://superuser.com/questions/651880/force-spin-down-of-external-hard-drive-on-linux-raspberry-pi

I have updated it to work with multiple drives placed in an array, and indexed by id so that any drive letter changes across reboots do not end up with us attempting to sleep different drives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment