Home > Archive > PERL Beginners > May 2004 > tail -f does not exit
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
tail -f does not exit
|
|
| Claude 2004-05-14, 5:30 am |
| I am reading continuously a file like this:
open LOG, "junk.txt" or die "Cannot open $file, $!\n";
while ( my $line = <LOG> ) {
print "$line";
}
While appending lines to the file from a shell command line:
$ echo "this is a new line" >> junk.txt
Everything ok, except that I would like to find out from the Perl code
above when junk.txt has been deleted, or renamed. Unfortunately, "tail
-f" does not exit...
Any idea how to do that?
--
Claude
| |
| Rob Dixon 2004-05-14, 7:30 am |
| Claude" <claude.b@luukku.com> wrote:
>
> I am reading continuously a file like this:
>
> open LOG, "junk.txt" or die "Cannot open $file, $!\n";
> while ( my $line = <LOG> ) {
> print "$line";
> }
>
> While appending lines to the file from a shell command line:
>
> $ echo "this is a new line" >> junk.txt
>
> Everything ok, except that I would like to find out from the Perl code
> above when junk.txt has been deleted, or renamed. Unfortunately, "tail
> -f" does not exit...
>
> Any idea how to do that?
Hi Claude,
You need to close and reopen the file if you want to check for a rename.
Something like the program below.
HTH,
Rob
use strict;
use warnings;
use IO::Handle;
autoflush STDOUT;
use Fcntl qw(:s );
my $file = 'junk.txt';
my $pos;
while (1) {
open LOG, $file or die "Cannot open $file, $!";
s LOG, $pos, SEEK_SET if defined $pos;
print while <LOG>;
$pos = tell LOG;
close LOG;
sleep 1;
}
| |
| Claude 2004-05-14, 11:30 am |
| >>>>> "Rob" == Rob Dixon <rob@dixon.port995.com> writes:
[...]
Rob> You need to close and reopen the file if you want to check for a
Rob> rename. Something like the program below.
[...]
Tx, Rob, I'll give feedback soon here!
--
Claude
| |
| Claude 2004-05-19, 8:30 am |
| >>>>> "Rob" == Rob Dixon <rob@dixon.port995.com> writes:
I got your code running nicely, although I had to make a small change due to
an older Perl (5.004) I am using:
[...]
Rob> You need to close and reopen the file if you want to check for a rename.
Rob> Something like the program below.
Which actually emulates "tail f-" nicely, since it notices a filename
change and exits, on contrary of "tail -f" which has to be killed.
Rob> use strict;
Rob> use warnings;
Rob> use IO::Handle;
Rob> autoflush STDOUT;
Rob> use Fcntl qw(:s );
Removed line, as s is not yet a tag in earlier Perl.
Rob> my $file = 'junk.txt';
Rob> my $pos;
Rob> while (1) {
Rob> open LOG, $file or die "Cannot open $file, $!";
Rob> s LOG, $pos, SEEK_SET if defined $pos;
s LOG, $pos, 0 if defined $pos;
Rob> print while <LOG>;
Rob> $pos = tell LOG;
Rob> close LOG;
Rob> sleep 1;
Rob> }
Tx a lot, Rob!
--
Claude
|
|
|
|
|