in reply to How to end a process started with open "cmd |" before it has output

Hey Crackers2, Wrap it in an eval with an alarm like so:
my $timeout = 5; eval { #traps fatal alarm.. local $SIG{ALRM} = sub { die "alarm\n" }; #\n required alarm $timeout; print "Do you suck (Y/N)?"; $answer = <>; alarm 0; }; + if (!($@)) { #looks like they typed something Y?... if ($answer =~ /[Nn]/) { # guess they dont suck $suck = 0; } else { #suck = 1; } } else { $suck = 1; print "You didn't type anything you suck..\n"; }
just replace the above $response = <> with your system call or whatever..


daN.
  • Comment on Re: How to end a process started with open "cmd |" before it has output
  • Download Code

Replies are listed 'Best First'.
Re: Re: How to end a process started with open "cmd |" before it has output
by Crackers2 (Parson) on May 07, 2004 at 03:01 UTC

    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; }