in reply to Re^3: Getting PID of process forked by backticks
in thread Getting PID of process forked by backticks

I had a similar requirement, and came up with this:

my %p; sub killchildren { my $ppid = shift; chomp(my $out = `ps -o pid --no-headers --ppid $ppid`); for (split /\n/, $out) { killchildren($_); kill 9, $_; } } sub homicide { killchildren($$); $p{fg}{timeout} = 1; } sub fg { my ($cmd, $timeout) = @_; $p{fg}{timeout} = 0; my $out; eval { local $SIG{ALRM} = q(homicide); alarm $timeout; $out = `$cmd`; alarm 0; }; return $out unless $p{fg}{timeout}; return q(TIMEOUT); } # # TEST # my $out = fg("echo Hi; sleep 5", 1); print "Out = $out\n"; $out = fg("echo Bye; sleep 5", 10); print "Out = $out\n";

The test example had the following output:

~/src$ ./a.pl
Out = TIMEOUT
Out = Bye