my $prg = "./runaway.pl"; # program that hangs, dies or exits 0 my $timeout = 4; # seconds to wait my $tries = 4; # number of tries my $run_ok = 0; # just another flag for my $try (0..$tries) { print "\n\n-----------------#$try\n"; if (run()) { print "--- run OK\n"; $run_ok = 1; last; } } unless ($run_ok) { die "---Ieerg!\n"; } #################### sub run { my $return_code = undef; my $pid = fork(); unless (defined $pid) { die "could not fork\n"; } if ($pid == 0) { # child print " child: exec\n"; exec("$prg arguments"); exit; } else { # parent print " parent: wait\n"; my $time = time(); my $kill_flag = 1; while (time() - $time < $timeout) { my $kid = waitpid(-1,WNOHANG); # needs a use POSIX ":sys_wait_h" if ($kid == -1) { $kill_flag = 0; $return_code = 1; last; } sleep 1; } kill(9, $pid) if $kill_flag; } return $return_code; }