Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

I can't get the sample code for File::Tail to read my file

by markseger (Beadle)
on Dec 21, 2011 at 19:28 UTC ( #944682=perlquestion: print w/ replies, xml ) Need Help??
markseger has asked for the wisdom of the Perl Monks concerning the following question:

I was going to write some code to tail a log file and detect when it rolled and then close/reopen it. Then I saw a reference to File::Tail and it sounded like just what I want. So I thought I'd try something very simple. I literally copied the sample usage script, pointed it to a file called /tmp/xxx with a few lines in it and nothing happens! I tried to echo some additional lines to the file they're not reported either. Could there be some versioning thing going on? I'm running perl 5.8.8 and File::Tail 0.99.3.
#!/usr/bin/perl -w use File::Tail; $file=File::Tail->new("/tmp/xxx"); while (defined($line=$file->read)) { print "$line"; }
When I run it there is no output. I even put a print right after the first call to make sure it gets to the while line and it does. Any ideas what I could try to see what's wrong?

I even tried eliminating the while and simply doing the read since I know there's something in the file:

use File::Tail; $file=File::Tail->new("/tmp/xxx"); print "opened\n"; $line=$file->read; print "after read\n"; print "$line";
and when I run it, it seems to be hanging on the read. The doc says you can pass it a non-zero value for debug so I tried 99 and got nothing. It feels like the file isn't getting opened but I have no ideas why.

Finally I tried getting even more creative with more instrumentation as well as some error handling.

use File::Tail; $file=File::Tail->new(name=>"/tmp/xxx", debug=>99, interval=>1, maxint +erval=>1) or print "ERROR1: $!\n"; print "ERROR2: $!\n"; print "opened\n"; $line=$file->read; print "after read\n"; print "$line";
It now looks like $! is getting set to "Inappropriate ioctl for device", but only when I report it with ERROR2 and not ERROR1. Is that message even real? In other words the constructor doesn't seem to be failing. I'm real confused by what I'm seeing.

-mark

Comment on I can't get the sample code for File::Tail to read my file
Select or Download Code
Re: I can't get the sample code for File::Tail to read my file
by toolic (Chancellor) on Dec 21, 2011 at 19:51 UTC
    When I run it there is no output.
    Same for me, if I point to a file which is static. However, I do get output if I point to a file which I know is currently growing in size (similar to tail -f).

    Here is a quote from the File::Tail POD (emphasis mine):

    File::Tail - Perl extension for reading from continously updated files
Re: I can't get the sample code for File::Tail to read my file
by Eliya (Vicar) on Dec 21, 2011 at 20:29 UTC
    ...pointed it to a file called /tmp/xxx with a few lines in it and nothing happens! I tried to echo some additional lines to the file they're not reported either.

    For the first part to work, you'd have to specify the tail option, e.g. tail => 10, to read/print out the last 10 lines of the file upon starting the script.

    The latter part should've worked, though (if you append to the file while the test script is running — which I suppose you did).   As I can't replicate the problem here, I can only suggest you try strace/truss/tusc to check what's going on (or isn't) underneath.

    For comparison, on my (Linux) system, I repeatedly (once a second) get the following block

    $ strace ./944682.pl ... nanosleep({1, 0}, {1, 0}) = 0 nanosleep({0, 0}, NULL) = 0 lseek(3, 0, SEEK_END) = 32 open("/tmp/xxx", O_RDONLY) = 4 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3d7d2c00) = -1 ENOTTY (I +nappropriate ioctl for device) lseek(4, 0, SEEK_CUR) = 0 fstat(4, {st_mode=S_IFREG|0644, st_size=32, ...}) = 0 fcntl(4, F_SETFD, FD_CLOEXEC) = 0 fstat(4, {st_mode=S_IFREG|0644, st_size=32, ...}) = 0 close(4) = 0 lseek(3, 0, SEEK_END) = 32 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 ...

    when there's no new lines in /tmp/xxx.  And an additional block

    ... lseek(3, 72, SEEK_SET) = 72 read(3, "foo\n", 4) = 4 lseek(3, 0, SEEK_CUR) = 76 rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0 rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 nanosleep({1, 0}, {1, 0}) = 0 nanosleep({0, 0}, NULL) = 0 write(1, "foo\n", 4foo ) = 4 ...

    when a new line has been appended to the file.

    BTW, the "Inappropriate ioctl for device" error is meaningless, i.e. to be expected, here.  This is also what you see in $!, when you check that variable even though the actual system/library call in question hasn't failed.  (In other words, you should only interpret that variable, if Perl actually did indicate a failure.)

Re: I can't get the sample code for File::Tail to read my file
by Sewi (Friar) on Dec 22, 2011 at 05:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://944682]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-09-22 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (186 votes), past polls