in reply to Opening a file at a designated offset based on closing of the file
here's a little script that does just that.
my $dhcplog = '/var/log/dhcpd'; my $lastfile = '/var/run/statdhcpcron.last'; my $logfile; open $logfile, '<', $dhcplog; sub setlast { my $last = shift; my $f; open $f, '>', $lastfile or return; print $f $last,$/; # last is first line report($f); # report is the rest of f +ile close $f; } sub getlast { local @ARGV = $lastfile; my $l = <>; # last is first line $l ||= 0; # or start from beginning $l = 0 if $l > -s $logfile; # handle rotate/truncate return $l; } my $last = getlast(); seek $logfile, $last, 0; while (<$logfile>) { # process lines } setlast(tell $logfile); # remember where we stopped close $logfile; # mail report if needed exit; sub report { # returns report based on lines processed }
don't forget to handle cases such as 'first time run' and 'file mysteriously shrunk'. this script keeps the last byte processed (and a copy of the report) in a file in /var/run.
|
|---|