That's actually how I started out, but it doesn't work in this case. If you try the below code, you'll notice it suffers the same problem as the code I posted before.

In this case, the alarm will fire and execute the die() statement, and the program will print out the "NONE" message, but then it will hang because the program will try to close the $fh filehandle when it exits. This will, as above, hang until some input is received.

use strict; use warnings; use IO::Select; my $an; my $timeout = 20; my $read_set = new IO::Select(); eval { local $SIG{ALRM} = sub { die "Timeout\n"; }; alarm $timeout; open my $fh, "tcpdump -nn -l -e dst 224.111.111.112 and not igmp 2>/ +dev/null |"; $read_set->add($fh); while (1) { my ($rh_set) = IO::Select->select($read_set, undef, undef, 1); if (defined $rh_set) { if ($_ = <$fh>) {; if (s/^\S* \S* M \S* \S* 0800 \S*: (\S*\.\S*\.\S*\.\S*)\.\S* > + \S*\.\S*:.*$/$1/s) { $an = $1; alarm 0; last; } } else { logwarn "EOF while reading from tcpdump. sleeping 2 seconds an +d restarting tcpdump"; $read_set->remove($fh); close $fh; sleep 2; open $fh, "tcpdump -nn -l -e dst 224.111.111.112 and not igmp +2>/dev/null |"; $read_set->add($fh); } } } close $fh; }; if ($@ && ($@ ne "Timeout\n")) { die $@; } if (defined $an) { print "$an\n"; exit 0; } else { print "NONE\n"; exit 1; }

In reply to Re: Re: How to end a process started with open "cmd |" before it has output by Crackers2
in thread How to end a process started with open "cmd |" before it has output by Crackers2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.