Home > Uncategorized > Howto: Automatically spin down external usb hard drives in Ubuntu

Howto: Automatically spin down external usb hard drives in Ubuntu


Spinning down external usb hard drives in Ubuntu doesn’t allways happen automatically in Ubuntu. To force a spindown, you can issue the command

sync
sdparm --flexible --command=stop /dev/sdX &>/dev/null

where you should replace the sdX entry by the correct name of your external hard drive (usually sdb, sdc or similar).
To do this automatically every 30 minutes, save the code below as e.g. ~/bin/spindown/spindown.sh.

# !/bin/sh

# Get new state from diskstats
NEWstate=$(cat /proc/diskstats | grep $1)
echo $NEWstate > NEWstate.txt

# compare md5 sums
md5new=$(md5sum NEWstate.txt | sed 's/ .*//')
md5old=$(md5sum OLDstate.txt | sed 's/ .*//')

# if no changes, power down
if [ "$md5new" = "$md5old" ]; then
	sdparm --flexible --command=stop /dev/$1 &>/dev/null
fi

# Write current state to file
echo $NEWstate > OLDstate.txt

Then, add the entry

*/30 *   * * *   root	sh /home/user/bin/spindown/spindown.sh sdX

to the cron file /etc/crontab. The script automatically checks to see if the drive has been active for the last 5 mins. If not, it forces a spindown.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
Categories: Uncategorized Tags:
  1. November 20th, 2009 at 07:14 | #1

    Hey, that’s what I have searched for a long time. Thanks, man. It works!

  2. January 7th, 2010 at 23:28 | #2

    Great post – works really well. Thanks.

    If anybody’s interested, I also added some code at the top of the script to check that it is being run as root…

    if [ "$(id -u)" != "0" ]; then
    echo “This script must be run as root” 1>&2
    exit 1
    fi

    Obviously this is not needed for cron, but useful for testing etc.

  3. March 2nd, 2010 at 19:31 | #3

    hey!

    many thanks for the script.

    for external usb drive compatibility i added the following lines.

    #convert UUID to ID
    disk=$(blkid | grep $1 | cut -c 6-9)

    #check if the disk uuid, or pieces of it were ok
    if [ ! -n $disk ]; then
    echo “no such disk – disk!”
    exit 1
    fi

    cheers

  4. Phil
    April 25th, 2010 at 10:44 | #4

    Are md5sum and sed really necessary? Could you not just do a diff between OLDstate.txt and NEWstate.txt?

    [ -z $(diff OLDstate.txt NEWstate.txt) ] && sdparm –flexible –command=stop /dev/$1

    Translation: Spin down when diff returns a zero-length string on comparing OLDstate.txt and NEWstate.txt. This gets rid of a few lines of code and reduces external dependencies.

  5. June 20th, 2010 at 12:59 | #5

    Awesome, just what I was looking for since my internal SATA HD’s would not spin down anymore after upgrade to ubuntu 10.04

  6. whackedout
    November 13th, 2010 at 20:11 | #6

    */5 * * * * root sh /home/user/bin/spindown/spindown.sh sdX

    should the sdX be sda, sdb, sdc etc or should it stay sdX

    thanks

  7. thinmintaddict
    December 10th, 2010 at 03:04 | #7

    When i run this command, the drive stops for a second and immediately spins back up. any ideas?

  8. Rena
    January 8th, 2011 at 17:44 | #8

    # !/bin/sh

    if [[ -e /dev/$1 ]]; then
    echo $1 Spin power down
    notify-send –icon=’/usr/share/icons/GartoonRedux/scalable/apps/gaim.svg’ “Spinning Powerdown: $1″
    else
    # Get new state from diskstats
    NEWstate=$(cat /proc/diskstats | grep $1)
    echo $NEWstate > /var/ds.state/NEWstate.$1

    # compare md5 sums
    md5new=$(md5sum /var/ds.state/NEWstate.$1 | sed ‘s/ .*//’)
    md5old=$(md5sum /var/ds.state/OLDstate.$1 | sed ‘s/ .*//’)

    # if no changes, power down
    if [ "$md5new" = "$md5old" ]; then
    sdparm –flexible –command=stop /dev/$1 &>/dev/null
    fi

    # Write current state to file
    echo $NEWstate > /var/ds.state/OLDstate.$1

    echo $1 Device Does Not Exist !
    exit 0
    fi

    So… i put a notify in the script and the oldstat,txt is replaced with oldstate.$ so will permit to use it on more than one disk as the extension will be .sdX and made a sort of check if the disk exist, was a good idea and a good job to start wirh, thanks

  9. Rena
    January 9th, 2011 at 17:27 | #9

    # !/bin/sh

    if [[ -e /dev/$1 ]]; then
    echo $1 Spin power down
    notify-send –icon=’/usr/share/icons/GartoonRedux/scalable/apps/gaim.svg’ “Spinning Powerdown: $1″
    # Get new state from diskstats
    NEWstate=$(cat /proc/diskstats | grep $1)
    echo $NEWstate > /var/ds.state/NEWstate.$1

    # compare md5 sums
    md5new=$(md5sum /var/ds.state/NEWstate.$1 | sed ‘s/ .*//’)
    md5old=$(md5sum /var/ds.state/OLDstate.$1 | sed ‘s/ .*//’)

    # if no changes, power down
    if [ "$md5new" = "$md5old" ]; then
    sdparm –flexible –command=stop /dev/$1 &>/dev/null
    fi

    # Write current state to file
    echo $NEWstate > /var/ds.state/OLDstate.$1
    else
    echo $1 Device Does Not Exist !
    exit 0
    fi

    ops i posted the wrong one… this one works

  10. admin
    January 15th, 2011 at 20:10 | #10

    @Rena

    Thank you, Rena.

  11. admin
    January 15th, 2011 at 20:10 | #11

    @whackedout
    sdX should be sda

  12. admin
    January 15th, 2011 at 20:15 | #12

    @thinmintaddict

    It sounds as if a process is accessing the drive while your are trying to spin it down.
    You can use ”fuser -m /path/to/device” to get a list of processes accessing the device,
    and ”fuser -km /path/to/device” to kill the processes. Check out fuser’s manpage for more options.

  13. Rena
    January 18th, 2011 at 01:11 | #13

    yes sdx could be sda sdb sde or so on, seems working fine here,but i found something similar:
    #!/bin/bash
    let a=0

    for i in `seq 0 100`
    do
    let a=`cat /proc/diskstats | grep “0 $1″ | awk ‘{print $(NF-2)}’`+a
    sleep 0.1s
    done
    echo $a
    if [ $a == 0 ]
    then
    sdparm -C stop /dev/$1
    fi
    exit 0

    seems working also, and no need to write down the stats….

  14. Rena
    January 18th, 2011 at 01:18 | #14

    Even if i can say that the disk is almost powerd on if you even access to the desktop, i dont see much usefull this can be, the drive goes on off too many times and this is harmfull for it.

    i just finished my netgear script today, nice stuff, keepd up the line at the max speed connection than can get on that moment and changes speed if the s/n or attenuation changes on the line to not break down,it’s a sort of stay on the max speed the line can support on that moment.
    running that for a few days now, and i believe who is far from the box it’s a need.

  15. admin
    January 18th, 2011 at 09:34 | #15

    @Rena
    If you run this every 0.1 seconds, you seriously risk damaging the hardware. My script does it once every 30 minutes.

  16. Rena
    January 18th, 2011 at 21:39 | #16

    you can always set 10 instead of 0.1…

  17. Cheez
    June 29th, 2011 at 01:47 | #17

    @admin
    This script is nice ’cause it makes sure that there is no activity within a 10 second period. Run it periodically as a cron job.

  18. Sorbus
    January 24th, 2012 at 01:43 | #18

    What would I need to do to spin two drives down?

    Cheers

    Sorbus

  1. No trackbacks yet.