Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Autoflushing revisited.

by rhowerton (Novice)
on Jul 11, 2003 at 15:53 UTC ( [id://273427]=perlquestion: print w/replies, xml ) Need Help??

rhowerton has asked for the wisdom of the Perl Monks concerning the following question:

(Bowing deeply, rhowerton humbly asks)
My understanding of autoflushing is that if you set $| = 1 then it is supposed to flush after a write (http://www.perlmonks.com/index.pl?node_id=20590), however it does not seem to be working. I am assuming that the print statement is a write and that it should print to the screen immediately upon execution. Here is the script I am trying to get working...understand it has changed many times as frustration has set in.
$|++; open(TCPDUMP,"/usr/sbin/tcpdump -i eth0:1 port 80 -s 60 -xX | grep GET + |") or die "Could not run command: $!"; my $DEBUG = 1; my $sleepTime = 5; my $count = 0; my $sleepCheck = 0; while(<TCPDUMP>) { chomp($_); print "$_\n"; FORK: { if($sleepCheck == 0) { if (my $pid = fork) { # parent here # child process pid is available in $p +id $count++; } elsif (defined $pid) { # $pid is zero here if defined # child here # parent process pid is available with + getppid $sleepCheck = 1; sleep($sleepTime); open(DUMPLOG, "> ./dump.log") or die " +Could not open dump log: $!"; print DUMPLOG "$count\n"; print "$count\n"; close(DUMPLOG); $sleepCheck = 0; $| = 0; exit(0); } elsif ($! == EAGAIN) { # EAGAIN is the supposedly recoverable + fork error if($DEBUG == 1) { print "Error: $!.\n"; } sleep 5; redo FORK; } else { # weird fork error die "Can't fork: $!\n"; } } else { $count++; } } }
Thank you in advance,
Ryan

Replies are listed 'Best First'.
Re: Autoflushing revisited.
by pzbagel (Chaplain) on Jul 11, 2003 at 16:31 UTC

    tcpdump also does output caching, so it waits until it has a certain amount of data before it prints it. Hence, even though perl is autoflushing, tcpdump isn't. No input, no output. Try the -l option to tcpdump to alleviate this issue. Also, man tcpdump

    HTH

Re: Autoflushing revisited.
by tedrek (Pilgrim) on Jul 11, 2003 at 16:35 UTC
    If I remember correctly, $| sets the currently selected handle to autoflush ie. STDOUT. This means TCPDUMP is not set to autoflush, and even if it was it only is automatic when writing to it. The problem is you can't make grep and/or tcpdump autoflush by setting $|, so you're program is writing to the screen as soon as it gets the information but that takes a bit because tcpdump/grep are buffering it. Take a look at the command line args for grep/tcpdump and see if you can turn off buffering.
Re: Autoflushing revisited.
by graff (Chancellor) on Jul 11, 2003 at 23:44 UTC
    Given the two previous replies, I would recommend that you take the "grep" part out of the command line pipe, and do the "grepping" in the perl script instead -- apparently, there is a command-line option for tcpdump to turn off output buffering, but I'm not aware of any such option for the standard "grep" utility. Since it's so easy to accomplish the same thing within Perl, simplify the command line.

    update: in case that wasn't clear, what I mean is:

    while (<TCPDUMP>) { next unless /GET/; ... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found