Skip to content

Instantly share code, notes, and snippets.

@cdfmr
Created October 15, 2013 12:18
Show Gist options
  • Save cdfmr/6990704 to your computer and use it in GitHub Desktop.
Save cdfmr/6990704 to your computer and use it in GitHub Desktop.
Spin down usb hard disk automatically if there is no activity | Depends: hdparm sdparm | Cron: */15 * * * * spindown.sh sda 10 (spin down sda if there is no activity for 10 minutes)
#!/bin/sh
DISK=sda
TIMEOUT=600
if [ $# -ge 1 ] ; then
DISK=$1
fi
if [ $# -ge 2 ] ; then
TIMEOUT=$2
TIMEOUT=$((TIMEOUT*60))
fi
status=`hdparm -C /dev/$DISK | grep -o standby`
if [ "$status" = "standby" ] ; then
exit 0
fi
for i in `seq 1 $TIMEOUT` ; do
io=`cat /proc/diskstats | grep "$DISK " | awk '{print $(NF-2)}'`
if [ $io -gt 0 ] ; then
exit 0
fi
sleep 1
done
logger "Sending standby command to /dev/$DISK"
sdparm -C stop /dev/$DISK > /dev/null
exit 0
@lzap
Copy link

lzap commented Mar 13, 2021

For the record, you need to pass -r (readonly) for modern disks:

sdparm -C stop -r /dev/sdd

@cdfmr
Copy link
Author

cdfmr commented Apr 6, 2021

For the record, you need to pass -r (readonly) for modern disks:

sdparm -C stop -r /dev/sdd

Thanks. The -r parameter is not available for old sdparm versions, at least not available for my openwrt router.

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