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(){ 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 }