RE: Tail -f and select - Again
[email protected] ("Mark Borghardt") Wed, 23 Jan 2002 10:19:17 -0700
| Newsgroups | perl.loop |
|---|---|
| Message-ID | <[email protected]> |
Thanks to all for all of the suggestions. There seem to be many modules
that address this issue, but I think I will stick to my external tail and
kill strategy. No external modules to load and I don't need the added
functionality that the modules provide. I'm sure it isn't the most
efficient, but I think it is the simplest.
That said, I am wondering why nobody has suggested using select and sysread.
I would have thought that this would have been a great way to solve the
problem. I tried the following code with no success:
#!/usr/local/bin/perl
$datafile="File1";
while () {
print"Opening $datafile\n";
open(DATAIN,"<$datafile");
vec($rin,fileno(DATAIN),1) = 1;
print "Fileno = ",fileno(DATAIN),"\n";
$ein=$rin;
$nfound=select($rout=$rin,undef,$eout=$ein,undef);
while () {
printf("nfound=%3x rout=%3x eout=%3x\n",$nfound,$rout,$eout);
sysread DATAIN, $datarec, 10;
print "rec->$datarec<-\n";
if (substr($datarec,0,4) eq "SPEC") {
$datafile=substr($datarec,4,5);
last;
}
$nfound=select($rout=$rin,undef,$eout=$ein,undef);
}
close(DATAIN);
}
The output and file data is attached at the bottom.
I don't understand why this wouldn't work. Shouldn't the select statement
inside the while loop wait only on filehandle 3 for data to be ready. This
is the behaviour I was looking for. The fact that nfound = 2 is also
confusing and why didn't the select block at the end of file 3?
I am running the script on an AIX box. Is there a problem with select on
some platforms or do I just misunderstand how select actually works.
All for now,
Mark Borghardt
Opening File1
Fileno = 3
nfound= 2 rout= 0 eout= 0
rec->Record 01
<-
nfound= 2 rout= 0 eout= 0
rec->Record 02
<-
nfound= 2 rout= 0 eout= 0
rec->SPECFile2
<-
Opening File2
Fileno = 3
nfound= 2 rout= 0 eout= 0
rec->Record 03
<-
nfound= 2 rout= 0 eout= 0
rec->Record 04
<-
nfound= 2 rout= 0 eout= 0
rec->Record 05
<-
nfound= 2 rout= 0 eout= 0
rec->SPECFile3
<-
Opening File3
Fileno = 3
nfound= 2 rout= 0 eout= 0
rec->Record 06
<-
nfound= 2 rout= 0 eout= 0
rec->Record 07
<-
nfound= 2 rout= 0 eout= 0
rec-><-
nfound= 2 rout= 0 eout= 0
rec-><-
nfound= 2 rout= 0 eout= 0
rec-><-
nfound= 2 rout= 0 eout= 0
rec-><-
nfound= 2 rout= 0 eout= 0
rec-><-
Data Files
File1
Record 01
Record 02
SPECFile2
File2
Record 03
Record 04
Record 05
SPECFile3
File3
Record 06
Record 07
-----Original Message-----
From: Uri Guttman [mailto:[email protected]]
Sent: Tuesday, January 22, 2002 11:51 AM
To: [email protected]
Cc: [email protected]; [email protected]
Subject: Re: Tail -f and select - Again
>>>>> "JS" == Jochen Stenzel <[email protected]> writes:
>> What I want to do is read data from a growing file (tail -f) until
>> a special record arrives, at which point I would like to close the
>> file and reopen another one repeating the process.
JS> there is a module called Event::File which implements tail -f the
JS> Event way.
and there is a Stem::LogTail module that implements tail the stem
way. :) the stem log subsystem supports filter rules which can easily
trigger a shutdown or restart of the tailing. no need for a separate
tail process.
uri
--
Uri Guttman ------ [email protected] --------
http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite
-
----- Stem and Perl Development, Systems Architecture, Design and Coding
----
Search or Offer Perl Jobs ----------------------------
http://jobs.perl.org