Jump to content

sergiu.topan

Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by sergiu.topan

  1. On 5/22/2020 at 4:43 PM, CS01-HS said:

    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.

    #!/bin/bash
    # 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
    drives=(
      "<DRIVE_IDENTIFIER_1>"
      "<DRIVE_IDENTIFIER_2>"
    )
    
    # spindown delay in minutes
    SPINDOWN_DELAY=30
    
    # Uncomment to enable debug mode
    #DEBUG=true
    
    # Set the directory where the status files will be stored,
    # /tmp/ is a fine default
    STATUS_DIR="/tmp"
    
    current=`date`
    
    # create status_dir if it doesn't exist
    mkdir -p ${STATUS_DIR}
    
    do_device() {
        local device_id=$1
        device=`ls -l /dev/disk/by-id/ | grep ${device_id} | head -1 | tail -c4`
        filename="${STATUS_DIR}/diskaccess-${device_id}.status"
    
        is_awake=`smartctl --nocheck standby -i /dev/${device} | grep 'Power mode is' | egrep -c 'ACTIVE|IDLE'`
        if [ "${is_awake}" == "1" ]; then
            if [ "$DEBUG" = true ]; then
                echo "${device} is awake"
            fi
    
            stat_new=$(grep "${device}1 " /proc/diskstats | tr -dc "[:digit:]")
    
            if [ ! -f "${filename}" ]; then
                echo ${current} "- ${filename} file does not exist; creating it now."
                echo ${stat_new} > ${filename}
            else
                stat_old=`cat ${filename} | tr -dc "[:digit:]"`
    
                # calculate minutes since last update to see if we should sleep
                current_time=$(date +%s)
                last_mod=$(stat ${filename} -c %Y)
                seconds_ago=$(expr $current_time - $last_mod)
                minutes_ago=$(expr $seconds_ago / 60)
    
                if [ "$DEBUG" = true ]; then
                    echo "${device} old stat: ${stat_old}"
                    echo "${device} new stat: ${stat_new}"
                    echo "${device} new stat modified ${minutes_ago} minutes ago"
                fi
    
                if [ "${stat_old}" == "${stat_new}" ]; then
                    if [ $minutes_ago -ge $SPINDOWN_DELAY ]; then
                        echo ${current} "- Drive /dev/${device} is awake and hasn't been used in ${minutes_ago} minutes; spinning down"
                        hdparm -y /dev/${device} > /dev/null 2>&1
                    else
                        echo ${current} "- Drive /dev/${device} was last used ${minutes_ago} minutes ago, less than spindown setting ($SPINDOWN_DELAY)"
                    fi
                else
                    echo ${current} "- Drive /dev/${device} has been used..."
                    echo ${stat_new} > ${filename}
                fi
            fi
        else
            if [ "$DEBUG" = true ]; then
                echo "${device} is asleep"
            fi
        fi
    }
    
    for device_id in ${drives[*]}
    do
        do_device ${device_id}
    done

     

     

    I tried this script and I noticed that doesn't detect properly when the drive is being used and every time I try to run it the drives are spinning down and up.

    I use unraid 6.12.6.

    After further investigation I changed 

    stat_new=$(grep "${device}1 " /proc/diskstats | tr -dc "[:digit:]")

    to 

    stat_new=$(grep "${device} " /proc/diskstats | tr -dc "[:digit:]")

    In this way it search for ativity on the drive not partition and like this is working properly.

    • Thanks 1
×
×
  • Create New...