Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # adjust the following three values to your needs
- DEV=sda
- IDLEMIN=10
- LOGFILE=/tmp/spindown.log
- type echo | grep -qi built || echo "Warning: echo is not a built-in command"
- STATFILE=/sys/block/$DEV/stat
- PERIOD=5
- let "CYCLES=$IDLEMIN*60/$PERIOD"
- state=active
- # keep 'sleep' executable in memory to prevent disk spin-up loading 'sleep'
- sleep 100d &
- rw=0
- count=0
- downtime=0
- read start rest < /proc/uptime
- start=${start%.*}
- echo "" >> $LOGFILE
- date "+%D %T - start monitoring of /dev/$DEV" >> $LOGFILE
- while true ; do
- rw_old=$rw
- read rw < $STATFILE
- if [ "$rw" != "$rw_old" ] ; then
- if [ "$state" = "standby" ] ; then
- read now rest < /proc/uptime
- now=${now%.*}
- let diffs="$now-$lastacc"
- date -d "$diffs seconds ago" "+%D %T - idle" >> $LOGFILE
- let diffs="$now-$idle"
- let "downtime+=$diffs"
- let "percent=($downtime*100)/($now-$start)"
- date -d "$diffs seconds ago" "+%D %T - spin down" >> $LOGFILE
- let "h=$diffs/3600" "m=($diffs/60)%60" "s=$diffs%60"
- date "+%D %T - spin up after ${h}h ${m}m ${s}s (${percent}%% down)" >> $LOGFILE
- fi
- count=0
- state=active
- read lastacc rest < /proc/uptime
- lastacc=${lastacc%.*}
- else
- if [ "$state" = "active" ] ; then
- let "count=$count+1"
- fi
- fi
- if [ "$state" = "active" -a $count -ge $CYCLES ] ; then
- #hdparm -Y /dev/$DEV # use this to actually spin down
- state=standby
- read idle rest < /proc/uptime
- idle=${idle%.*}
- fi
- sleep $PERIOD
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement