Turn off external hard drive if not used?
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2013
    Location
    Germany
    Posts
    210

    Turn off external hard drive if not used?

    Hi,

    I use my old notebook as a server. I added an external HD that I access via SMB. The problem is, the HD spins constantly, even if I don't use the server for a longer period. I heart that external HDD's are not made for 24/7 run times. Is it possible to turn off the hard drive automatically to protect it from overuse? I don't want to turn it on and off manually.

    kind regards

  2. #2
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,537

    Re: Turn off external hard drive if not used?

    I have 5 disks on my system. Here is what I use for turning off (sleeping a drive).

    Code:
    #!/bin/bash
    # sleepx.sh version .9   Date 2018/02/12
    # Do what you want to improve, use, destroy, laugh at
    #
    # Purpose
    #---------
    # When you have multiple drives on your computer but you 
    # use only one or two at a time. There is no need to keep
    #the unmounted (unused) drives spinning.
    # As root, if the drive is not in sleep mode, make it so
    # You may do what you want to improve the script.
    #
    #HOW TO USE
    #----------
    #sudo sleepx.sh a  c # to put drives a and c to sleep and show status
    #sudo sleepx.sh a  c # if drives are asleep, show them
    #sleepx.sh           # no arguments, show all drives in sleep mode 
    #sleepx.sh  a c      # as normal user, show only drives a and c (you can't set them as normal user)
    #need root privilege if you want to set a drive to sleep
    #-----------------------------------------------------------------------------------------------------------------------------------------------------
    # I setup a root crontab entry to check if the drives I chose are sleeping, and if not, put them to sleep again
    # my root crontab entry ...
    #*/20    *       *       *       *       /bin/date >> /root/sleepx.log && /usr/local/bin/sleepx.sh a c d  >>  /root/sleepx.log 
    #--------------------------------------------------------------------------------------------------------------------------------------------------------
    PATH=$PATH:/sbin:/usr/bin
    HDPARM=$(which hdparm)
    HDDTEMP=$(which hddtemp)
    GREP='grep  -F'
    rc=0
    SLEEP=veille                            #French
    SLEEP=sleeping
    if [ ! -e $HDPARM ]
    then
        rc=1
    fi
    if [ ! -e $HDDTEMP ] 
    then
        rc=1
    fi
    
    if [[ $rc == 1 ]]
    then
       echo "rc=$? Please install hdparm and hddtemp" 
       exit 1
    fi
    
    function usage()
    { 
      me=$(basename $0)
      iam=$(dirname $0 )
       echo -e "$0 is executed from $iam\nUse with  root access as:"  
       echo "$0 a b c ...  where a b c are drive letters."            
       echo -e "  Used to spin down drives to sleep mode.\n\n"        
       echo "  When you have multiple drives on your computer but"    
       echo "  you use only one or two at a time, there is no need to"
       echo "  keep the unused drive spinning. If you call the script"
       echo "  as root, you can turn off a list of drives or "          
       echo "  review which (by list or no list) drives are in sleep mode" 
       echo ""                                                          
       echo "Use without root access"                                   
       echo "$0 with no parameters display the sleep mode of all drives"  
       echo "$0 with parameters displays the sleep mode of the drives listed" 
       echo "An example of my root crontab entry follows.... " 
       echo "*/30 * * * * $iam/$me  c d e #my 3 drives not currently in use" 
       exit 1
    }
    
    if [[ $# == 1 && $1 == '-h' ]]; then
           usage
    fi
    
    
    if [[ $UID == 0 ]]; then
       if [[ $# > 0 ]]; then
            for  i in $@
            do
                    SDX="/dev/sd${i} "
                    rc=$(2>&1  ${HDDTEMP} ${SDX}  | $GREP -c   $SLEEP ) #count occurrances
                    if [[ $rc == 0 ]]; then                         # $SLEEP not found
                       #echo "drive $i is out of sleep"
                       ${HDPARM} -q -S 12  ${SDX}   >/dev/null #60=5x12 seconds
                       ${HDPARM} -q -y     ${SDX}   >/dev/null
    
                    fi
                    2>&1 $HDDTEMP  ${SDX}  | $GREP $SLEEP | cut -d: -f1,3 
            done
       else
            2>&1 $HDDTEMP | $GREP $SLEEP | cut -d: -f1,3                         
       fi        
    #####################
    #       regular user
    ####################
    else
            if [[  $# > 0 ]];then
                    for i in $@
                    do
                        echo else
                        2>&1 $HDDTEMP  /dev/sd$i | $GREP $SLEEP | cut -d: -f1,3
                    done
            else
                    2>&1 $HDDTEMP  | $GREP $SLEEP  | cut -d: -f1,3            
            fi
    #
    fi
            exit 0
    #
    While it works for my internal disks, you may be surprised to find that the external hard drive may operate differently
    Good luck
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  3. #3
    Join Date
    Oct 2011
    Posts
    1,895

    Re: Turn off external hard drive if not used?

    hd-idle should do what you want, you can find it in Fedora repository.

  4. #4
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,537

    Re: Turn off external hard drive if not used?

    Interesting, hd-idle is doing an equivalent drive action, specific per drive, to roughly what the above script is doing with the lines.
    HDPARM documentation indicates that the shutdown time is a number that you multiply by 5. So, setting a 12 as shown below actually results in a shutdown time of 60 seconds.

    ${HDPARM} -q -S 12 ${SDX} >/dev/null #60=5x12 seconds
    ${HDPARM} -q -y ${SDX} >/dev/null

    hdtemp will tell you about the drive (if it is active or spinning).

    The way I am configured for my desktop is as follows:
    /dev/sda has a backup that I normally set to sleep, until needed
    /dev/sdb, /dev/sdc are two drives that I do not use all the time, and they are set to sleep
    /dev/sdd has my global $HOME
    /dev/sde has my SSD Fedora 27

    When I use F27 KDE on /dev/sde
    then drives a,b,c,e are set to idle.

    when I use F28 Gnome Beta on /dev/sdc
    then drives a,b,e are set to idle.

    etc.

    I use crontab to run the above script every 20 minutes. I suppose I could check less often. I will be setting up a test with /dev/sdf (a plugged in external drive)

    In my experience with Seagate drives. the case and drive are sealed. I know not if the drive in that sealed case is a normal retail drive or stripped down.

    More than likely, I will check out hd-idle and add it to my root crontab. That will allow me to dispense with my own script.
    Last edited by lsatenstein; 6th March 2018 at 05:48 PM.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  5. #5
    Join Date
    Oct 2011
    Posts
    1,895

    Re: Turn off external hard drive if not used?

    The package has a systemd service included. Configuration for the service can be found in /etc/sysconfig/hd-idle.

Similar Threads

  1. How to turn off usb hard drive automatically?
    By tonytiger in forum Hardware & Laptops
    Replies: 7
    Last Post: 16th December 2008, 11:04 PM
  2. Hard drive dma issues (can't turn on DMA)
    By Gustavo Melo in forum Hardware & Laptops
    Replies: 1
    Last Post: 5th November 2006, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •