#12 2012-01-30 22:42:37
Re: 外接USB硬碟,有無方法可以關閉電源進入省電模式
下面的腳本只適用在核心2.4版,有沒有大大可以幫忙改一下,讓他可以用在核心2.6版,謝謝!
#!/bin/sh
if /usr/bin/[ $# -ne 1 ]; then
/bin/echo 1>&2 "Usage: $0 "
exit 1
fi
PERIOD=30
BOUNDARY=1200
SCSISTOP=/usr/local/sbin/scsi-stop
LOGFILE=/tmp/scsi-stop.log
searchstr="disk_io:"
devaddr="(`/bin/ls -l $1 | /usr/bin/awk '{print($5 int($6/16))}'`)"
str=`/bin/grep "$searchstr" /proc/stat | devaddr="$devaddr" /usr/bin/awk -v 'RS= |\n' -F : '{ if($1==ENVIRON["devaddr"]) print($2) }'`
cooltime=0
stopped=0
while /bin/true; do
/bin/sleep $PERIOD
newstr=`/bin/grep "$searchstr" /proc/stat | devaddr="$devaddr" /usr/bin/awk -v 'RS= |\n' -F : '{ if($1==ENVIRON["devaddr"]) print($2) }'`
if /usr/bin/[ "$newstr" = "$str" ]; then
cooltime=$(($cooltime + $PERIOD))
if /usr/bin/[ $cooltime -ge $BOUNDARY -a $stopped -eq 0 ]; then
$SCSISTOP $1
time=`/bin/date '+%b %e %H:%M:%S'`
/bin/echo "$time: Cooled down..." >> $LOGFILE
stopped=1
fi
else
if /usr/bin/[ $cooltime -ne 0 ]; then
time=`/bin/date '+%b %e %H:%M:%S'`
/bin/echo "$time: Disk first used after $cooltime seconds" >> $LOGFILE
fi
cooltime=0
str=$newstr
stopped=0
fi
done
目前是使用 http://tomatousb.org/forum/t-266375/ 的腳本
#!/bin/sh
# author: karog
# date: 17 Sep 2010
# ***USE AT YOUR OWN RISK***
# script name
SCRIPTNAME="${0##*/}"
# log /var/log/messages and add -s for stderr
log ()
{
logger -p user.info -t "$SCRIPTNAME" "$1"
}
# usage
if [ $# -eq 0 ]; then
log "Usage: $SCRIPTNAME device [idletime [checktime]] &"
log " device /dev/sd[a-z]"
log " idletime optional idle time in seconds before disk spindown (default 900)"
log " checktime optional time in seconds to sleep between idle checks (default 30)"
exit 0
fi
DEVICE="$1"
# validate form as /dev/sd[a-z]
echo "$DEVICE" | grep -q "^/dev/sd[a-z]$"
if [ $? -ne 0 ]; then
log "Invalid device not of form /dev/sd[a-z]: $DEVICE; exiting"
exit 1
fi
# validate in the file system
ls "$DEVICE" > /dev/null 2>&1
if [ $? -ne 0 ]; then
log "Invalid device not in file system: $DEVICE; exiting"
exit 1
fi
# idle time
IDLETIME=900
IDLETIME_MIN=300
if [ $# -gt 1 ]; then
echo "$2" | grep -qE "^[[:digit:]]+$"
if [ $? -eq 0 ]; then
IDLETIME=$2
if [ "$IDLETIME" -lt "$IDLETIME_MIN" ]; then
log "IDLETIME set too small which is bad for disks: $IDLETIME; setting to minimum $IDLETIME_MIN"
IDLETIME="$IDLETIME_MIN"
fi
else
log "Invalid idletime: $2; using default $IDLETIME"
fi
fi
# check time
CHECKTIME=30
CHECKTIME_MIN=5
if [ $# -gt 2 ]; then
echo "$3" | grep -qE "^[[:digit:]]+$"
if [ $? -eq 0 ]; then
CHECKTIME=$3
if [ "$CHECKTIME" -lt "$CHECKTIME_MIN" ]; then
log "CHECKTIME set too small: $CHECKTIME; setting to minimum $CHECKTIME_MIN"
CHECKTIME="$CHECKTIME_MIN"
fi
else
log "Invalid checktime: $3; using default $CHECKTIME"
fi
fi
SPINNING=1
STOPPED=0
DEVNAME=${DEVICE##/dev/}
DEVSTATS=`cat /proc/diskstats | grep " $DEVNAME "`
DEVIDLE=0
DEVSTATE="$SPINNING"
log "running"
# forever
while true; do
sleep "$CHECKTIME"
NEWSTATS=`cat /proc/diskstats | grep " $DEVNAME "`
IOINPROGRESS=`echo $NEWSTATS | cut -d ' ' -f 12`
# if no I/O in progress and stats have not changed
if [ "$IOINPROGRESS" -eq 0 -a "$NEWSTATS" = "$DEVSTATS" ]; then
# no disk activity
let "DEVIDLE+=CHECKTIME"
# test idle time exceeded and disk spinning
if [ "$IDLETIME" -le "$DEVIDLE" -a "$DEVSTATE" -eq "$SPINNING" ]; then
scsi-stop "$DEVICE"
DEVSTATE="$STOPPED"
log "$DEVICE spinning down"
fi
else # disk activity
# if disk was stopped
if [ "$DEVSTATE" -eq "$STOPPED" ]; then
DEVSTATE="$SPINNING"
let "SECS=DEVIDLE-IDLETIME"
let "DAYS=SECS/86400,SECS-=DAYS*86400"
let "HOURS=SECS/3600,SECS-=HOURS*3600"
let "MINS=SECS/60,SECS-=MINS*60"
if [ "$DAYS" -eq 0 ]; then DAYS=""; else DAYS="$DAYS days "; fi
if [ "$HOURS" -eq 0 ]; then HOURS=""; else HOURS="$HOURS hrs "; fi
if [ "$MINS" -eq 0 ]; then MINS=""; else MINS="$MINS mins "; fi
if [ "$SECS" -eq 0 ]; then SECS=""; else SECS="$SECS secs "; fi
log "$DEVICE spinning up after $DAYS$HOURS$MINS$SECS"
fi
DEVIDLE=0
DEVSTATS="$NEWSTATS"
fi
done
最後修改: ardayang (2012-01-30 23:40:39)
離線