in reply to Read a process output asynchronously
my @pslist = (); while (!@pslist) { local $SIG{ALRM} = { die "TIMEOUT" }; my $pid; eval { alarm 5; # arrange for a wake-up call in 5 seconds $pid = open(PSLIST, " ssh -2 $user\@ $peer ./pslist | ") || die "Remote process list gathering error:\n$!.\n"; while(<PSLIST>){ chomp($_); push(@pslist, $_); } close(PSLIST); alarm 0; # clear the alarm, if we got this far ok } if ($@ =~ /TIMEOUT/) { # abort the attempt, and try again kill 'TERM' => $pid; @pslist = (); } elsif ($@) { die $@; } } foreach (@service){ # .. check the existence of a process }
There is also at least one module on cpan for doing timeouts... but it's the sort of thing that is simple enough that you usually just wanna do it inline, rather than bringing in a module for it.
|
|---|