特殊:Badtitle/NS100:ExternalDriveStandby

来自Ubuntu中文
Wikibot留言 | 贡献2008年5月9日 (五) 18:56的版本 (新页面: {{From|https://help.ubuntu.com/community/ExternalDriveStandby}} {{Languages|UbuntuHelp:ExternalDriveStandby}} == Automatic Spin-Down For Idle External Hard Drives == It is often desirable...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航跳到搜索

{{#ifexist: :ExternalDriveStandby/zh | | {{#ifexist: ExternalDriveStandby/zh | | {{#ifeq: {{#titleparts:ExternalDriveStandby|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:ExternalDriveStandby|1|-1|}} | zh | | }}

Automatic Spin-Down For Idle External Hard Drives

It is often desirable to have external hard drives - such as those attached by USB and Firewire - spin down into a power-saving or 'standby' mode after a certain period of inactivity. Since these external drives cannot have their standby parameters set with 'hdparm' (which only handles IDE devices and not the pseudo-SCSI ones), some way other than the built-in method is required to make these drives spin down.

Requirements

K/Ubuntu 7.04 or later; The 'sg3-utils' package.

The Script

Save the following code as 'scsi-idle' and set the execute flag for it:

#!/bin/bash

declare -a DID
declare -a DSTATUS
declare -a DTIME
declare -a DOFF


interval=$1
sleepval=120
buspath=/sys/bus/scsi/devices

while [ true ]; do
thetime=`date +%s`

for scsidev in $( ls --format=across $buspath ); do
   if devenum="$( ls ${buspath}/${scsidev} | grep 'block:' )"; then
      devdir=$buspath/$scsidev/$devenum
      read devsize < ${devdir}/size
      read devremove < ${devdir}/removable

      if [ $devremove -eq 0 ] && [ $devsize -gt 0 ]; then # device is the right type
         read newstat < ${devdir}/stat
         hhit=0; listlen=${#DID[@]}  #; echo $listlen $scsidev

         for ((i=0;i<$listlen;i++)); do
            if [ ${DID[$i]} = $scsidev ]; then
               if [ "${DSTATUS[$i]}" = "$newstat" ]; then
                  if [ $[ ${thetime} - ${DTIME[$i]} ] -ge $interval ] \
                  && [ ${DOFF[$i]} -eq 0 ]; then
                     sg_start --pc=5 /dev/${devenum:6} # put into sleep mode
                   # sg_start --stop /dev/${devenum:6} # alternate method
                     echo stop /dev/${devenum:6}
                     DTIME[$i]=$thetime
                     DOFF[$i]=1
                  fi
               else
                  DSTATUS[$i]=$newstat
                  DTIME[$i]=$thetime
                  DOFF[$i]=0
               fi
               hhit=1; break
            fi
         done
         if [ $hhit -eq 0 ]; then   # add new drive to watch list
            DID[$listlen]=$scsidev
            DSTATUS[$listlen]=$newstat
            DTIME[$listlen]=`date +%s`
            DOFF[$listlen]=0
         fi
      fi
   fi
done
sleep $sleepval
done

Run it in the background like this:

$ sudo ./scsi-idle 1500 &

The above would cause external drives to spin-down after 25-27 min of inactivity. Even though 25 min (1500 sec) is specified, the internal delay loop can add a couple minutes to the reaction time. Enjoy!