in reply to command timeout not working
It might be easier (better?) to just dump the alarm completely, and use select(). That will take care of any safe signals issue, and allows you a bit more control as well
Something like this:
use strict; use warnings; use IO::Select; my $timeout = 20; my $read_set = new IO::Select(); my $pid; my $starttime = time(); my $pid = open my $fh, "-|", $command or die $!; $read_set->add($fh); while (time() - starttime < $timeout) { my ($rh_set) = IO::Select->select($read_set, undef, undef, 1); if (defined $rh_set) { # note: use sysread() instead of <> to be really safe if ($_ = <$fh>) { push @array, $_; print "+"; } else { # eof last; } } else { # nothing received for a second print "."; } } kill 9, $pid; close $fh; if (time() - starttime >= $timeout) { print "Timed out\n"; }
Update:Use time() instead of localtime()
Update 2:Fixed open() line
|
|---|