#!/usr/bin/env perl # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # Copyright (c) Benoit Sauvin 30 Septembre 2007 # # # Intent: keep all currently mounted USB drives alive (they doze at inconvenient times) # # Setup prerequisites: the existence of ~/bin and ~/bin/log directories # # ======================================================================================= # find out what drives are mounted by scanning output from system command "mount" # NB: "device" means /dev/sd[a-z] and NOT /dev/sd[a-z][0-9] use strict; my %device; for (`mount`) { next if !/\/dev\/sd([a-z])\d/; ++$device{$1}; } # for each device, get its udevinfo, looking specifically for serial number and bus type # do nothing if the device is NOT a USB drive # send sdparm command START otherwise to device, grab its output and note in log file foreach my $box (keys %device) { my $serial = ""; for (`udevinfo --query=env --name=/dev/sd$box`) { chomp; print "$_\n"; if (/^ID_SERIAL=(.*)$/) { $serial=$1; } if (/^ID_BUS=usb/) { my $s = `sdparm --command=start /dev/sd$box`; my $when = localtime; chomp $s; open file,">>~/bin/log/keepalive"; print file "/dev/sd$box $when $s (serial=$serial)\n"; close file; } } }