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

If that's what you want, I'd recommend using fork. I did something similar a while back, using fork, as described in Timing and timing out Unix commands. If you get it working without using fork, I'd be interested in seeing your code.

Replies are listed 'Best First'.
Re^4: Getting PID of process forked by backticks
by Anonymous Monk on Dec 14, 2012 at 20:38 UTC

    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