Jump to content

Script to spindown unassigned USB HDDs

1 post in this topic Last Reply

Recommended Posts

Posted (edited)

I have a USB HDD in unassigned devices which I use for backups. The drive won't spin down for whatever reason, mounted or not.


My solution was a modified version of this script that I have User Scripts run every hour.

It takes a list of drives, checks for activity and spins them down if they're inactive and spun up.


I thought I'd share it.


NOTE: The green "led" in Unassigned Devices stays green even when the drive's spun down, I don't know how or if it detects power status.

# This script looks for recent disk access, and if nothing has changed, puts /dev/disk/by-id/${drive} into spindown mode.
# Set your drive identifiers (listed in /dev/disk/by-id/) ignoring characters after the last "-"
# e.g. listing: usb-WD_My_Passport_25E2_75831314363630505A37-0:0
#      becomes: usb-WD_My_Passport_25E2_75831314363630505A37

# Set the directory where drive status files will be stored.
# /tmp/ would be standard but that may wake the array
# so maybe somewhere under /mnt/cache is better


# create status_dir if it doesn't exist
mkdir -p ${status_dir}

for device_id in ${drives[*]}
    device=`ls -l /dev/disk/by-id/ | grep ${device_id} | head -1 | tail -c4`
    stat_new=`cat /sys/block/${device}/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: smartctl --nocheck standby -i /dev/${device} | grep 'Power mode is'"
                power_mode=`smartctl --nocheck standby -i /dev/${device} | grep 'Power mode is'`
                echo ${current} "- ${power_mode}"
            is_awake=`smartctl --nocheck standby -i /dev/${device} | grep 'Power mode is' | egrep -c 'ACTIVE|IDLE'`
            if [ "${is_awake}" == "1" ]; then
                echo ${current} "- Drive /dev/${device} is awake but hasn't been used; spinning down"
                hdparm -y /dev/${device} > /dev/null 2>&1
            echo ${current} "- Drive /dev/${device} has been used..."
            echo ${stat_new} > ${filename}
        echo ${current} "- ${filename} file does not exist; creating it now."
        echo ${stat_new} > ${filename}



Edited by CS01-HS

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.