Tell and Seek
Test Perl code illustrating how to read a file from the position last read to. Handy for log processing agents run multiple times on a growing file, where repeated scans would otherwise duplicate previous matches. Re-reads entire file if last position past end of current file contents.
#!/usr/bin/perl use Fatal qw(open); # filename => last read offset my %file_position_stash = ( test => 5 ); my $file = shift || die "Usage: $0 filename\n"; open my $fh, '<', $file; # Try to resume where left off if ( exists $file_position_stash{$file} ) { seek $fh, $file_position_stash{$file}, 0 or warn "whoa: $!\n"; # If at end of file already, file truncated # since last read? Start from beginning, unless # file same size as last read position. if ( eof $fh and $file_position_stash{$file} != -s $fh ) { seek $fh, 0, 0; } } while (<$fh>) { print; } # Save where read to $file_position_stash{$file} = tell $fh or warn "whoa: $!\n"; use Data::Dumper; warn Dumper \%file_position_stash;
If possible, avoid copying and truncating log files. Instead, use software such as httplog to direct logs into files by date-based patterns.