Crackers2 has asked for the wisdom of the Perl Monks concerning the following question:
file: child.pl
#!/usr/bin/perl $| = 1; sleep 10; print "x\n"; while (1) { sleep 10; }
file: main.pl
#!/usr/bin/perl open my $fh, "./child.pl |"; close $fh;
Running mail.pl reliably exits after 10 seconds, i.e. right after child.pl has sent its first output. If you remove the print line from child.pl, the main program will never exit.
I'm interested in two things: an explanation of why the program behaves this way, and a solution to the concrete problem this is causing me, described below.
What I want is to either capture the first packet that passes the tcpdump filter ($an set to the source IP), or if no packet comes in before the timeout is over, leave the loop with $an still undefined.... my $active = 1; my $timeout = 20; my $an; open my $fh, "tcpdump -nn -l -e dst 224.111.111.112 and not igmp |"; $read_set->add($fh); while ($active) { my ($rh_set) = IO::Select->select($read_set, undef, undef, 1); if (defined $rh_set) { if ($_ = <$fh>) { # do some parsing on the line $an = $1; last; } else { # tcpdump died. real code will sleep 2 seconds # and then close and reopen $fh here } } if (--$timeout == 0) { $active=0; } } close $fh;
Currently I'm working around this problem by explicitely starting tcpdump in a fork()ed child and killing it after the timeout. This works but I have to use a temp file or a pipe to store the tcpdump output, and it's a lot more code than something like the above.
I'm open to whatever solutions the wise monks can come up with, even if it means going about it a whole other way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to end a process started with open "cmd |" before it has output
by ysth (Canon) on May 07, 2004 at 00:46 UTC | |
by Crackers2 (Parson) on May 07, 2004 at 03:09 UTC | |
|
Re: How to end a process started with open "cmd |" before it has output
by Zaxo (Archbishop) on May 07, 2004 at 01:39 UTC | |
by ysth (Canon) on May 07, 2004 at 06:25 UTC | |
|
Re: How to end a process started with open "cmd |" before it has output
by mutated (Monk) on May 07, 2004 at 00:45 UTC | |
by Crackers2 (Parson) on May 07, 2004 at 03:01 UTC |