Skip to content

Instantly share code, notes, and snippets.

@viljoviitanen
Last active December 11, 2015 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viljoviitanen/4570091 to your computer and use it in GitHub Desktop.
Save viljoviitanen/4570091 to your computer and use it in GitHub Desktop.
Spins down an idle disc. Made for raspberry pi raspbian wheezy, and my external usb drive which spins down with the command "sg_start -r -S".
#!/bin/sh
#spins down a disk. put this in /etc/cron.hourly so that it runs last
#cronjobs are run in alphabetical order so zzspindown is a sure bet...
#licence: public domain
DISK=sda
SGSTART=/usr/bin/sg_start
if [ ! -x $SGSTART ]
then
echo "$SGSTART missing. aborting."
exit 1
fi
PREVIOUS=/run/.spindown-$DISK
#to prevent errors from the first run, create the file
if [ ! -f $PREVIOUS ]
then
touch $PREVIOUS
fi
# Then see if there's activity in between this run and the previous run.
# If not, spin down.
line1=`cat $PREVIOUS`
line2=`grep " $DISK " /proc/diskstats`
echo "$line2" > $PREVIOUS
if [ "x$line1" = "x$line2" ]
then
$SGSTART -r -S /dev/$DISK
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment