in reply to eval leaving processess behind

my $cmd = ...; my $pid; my $fh; my @out; eval { local $SIG{ALRM} = sub { die "Timeout\n" }; alarm 5; $pid = open($fh, '-|', $cmd) or die; @out = <$fh>; undef $fh; alarm 0; }; kill TERM => $pid if defined($fh);

Replies are listed 'Best First'.
Re^2: eval leaving processess behind
by leonidlm (Pilgrim) on Nov 09, 2008 at 21:10 UTC
    Very nice, thank you.
    But is there a more "elegant way" of doing this ? In your solution I will fork another perl instance for each task, I am not sure it is the most efficient solution.

      q{perl -e'sleep 60*60'} was just the command I used for testing. You'd continue to use the same command as before. I've altered the post execute $cmd as your snippet did.

        Ohh sorry, my bad. Thank you again.