#!/usr/bin/ruby # This Ruby script uses sg3_utils (http://sg.torque.net/sg/) to # automatically put a SCSI device into standby mode after # a configurable idle time period. This also works with USB # storage devices (tested with Linux Kernel 2.6.14, sg3_utils 1.11 # on Gentoo Linux with an NEC PCI USB 2.0 controller # and an external Samsung SP2514N hard disk in an ICY Box. # Author: Steffen Rusitschka # Version: 0.1 # Homepage: http://rusi.is-a-geek.org # time in seconds of device inactivity until it will be # put to standby mode (15 minutes) IDLE_UNTIL_STANDBY = 15 * 60 # device to use DEVICE = "sda" lastRequestCount = 0 lastAccessTime = 0 sleepTime = 0 while true f = File.open('/sys/block/' + DEVICE + '/device/iorequest_cnt', 'r'); requestCount = f.readline.chomp.hex f.close now = Time.now.to_f if requestCount != lastRequestCount lastAccessTime = now end # if we are after the timeout and there were new # accesses after the last sleep time -> go to sleep (again) if now > lastAccessTime + IDLE_UNTIL_STANDBY \ && lastAccessTime > sleepTime sleepTime = now system '/usr/bin/sg_start 0 -pc=3 /dev/' + DEVICE # increase the request count by one for the command we just sent requestCount += 1 end lastRequestCount = requestCount # sleep 1 minute sleep 60 end