#!/usr/local/bin/perl # # monitor -- monitor access-times of a list of files, displaying # how many times each file is accessed # # John Harper # # $Id: monitor,v 1.1 1999/08/31 13:45:33 john Exp $ # # monitor OPTIONS... FILES... # # where OPTIONS are any of # # -t Display time of each access # -x COMMAND Execute COMMAND after each access # # and FILES is a list of filenames. use Getopt::Std; @files = (); @counts = (); @atimes = (); $command = 0; $delay = 15; sub get_atime { my $file = shift; my @stats = stat ($file); return $stats[8]; } if (! getopts('tx:')) { print "usage: monitor [-t] [-x COMMAND] FILE:COUNT...\n"; exit 5; } if (defined $opt_x) { $command = $opt_x; } for (my $i = 0; $i <= $#ARGV; $i++) { my $file = $ARGV[$i]; my $count = 0; if ($file =~ /^(.*):([0-9]+)$/) { $file = $1; $count = $2; } push (@files, $file); push (@counts, $count); push (@atimes, get_atime ($file)); } while (1) { for (my $i = 0; $i <= $#files; $i++) { my $atime = get_atime ($files[$i]); if ($atime > $atimes[$i]) { $atimes[$i] = $atime; $counts[$i]++; if (defined $opt_t) { my @time = localtime (); printf "%02d:%02d ", $time[2], $time[1]; } print " ** $files[$i] : $counts[$i]\n"; if ($command) { system ("$command &"); } } } sleep ($delay); }