I came across this looking to do the same thing. Unfortunately, none of the posted solutions worked correctly for me. After testing, I found that it is essential to use "setpgrp(0,0)" in the child, otherwise the kill seems to be ignored. Also, it's unclear what would happen if the child has exec'd something which is hung in an unkillable fashion, so I'll attach a couple of working solutions here.
First solution does an alarm and a kill, and doesn't use any fancy libraries.
#!/usr/bin/perl
use strict;
sub sys_alarm {
my ( $cmd_result, $pid );
my $cmd = shift( @_ );
my $timeout = shift( @_ );
eval {
local $SIG{ ALRM } = sub {
kill -9, $pid;
warn "timeout of command \"$cmd\"; killing $pid\n";
};
if ($pid = open(IN, "-|")) {
alarm $timeout;
while ( <IN>) {
$cmd_result .= $_;
}
close(IN);
waitpid $pid, 0;
alarm 0;
} else {
die "cannot fork: $!" unless defined $pid;
setpgrp( 0, 0 );
exec( $cmd );
exit;
}
};
return $cmd_result;
}
my $x = sys_alarm("sleep 10; echo foo", 5);
print "Output: " . $x . "\n\n";
The second solution simply alarms and moves on, and leaves the child to do whatever it will. This may be preferable if there is a potential of unkillable processes. I have the impression that the previous code will not move on if the child does not terminate upon being killed.
#!/usr/bin/perl
use strict;
sub sys_alarm {
my $cmd_result;
my $cmd = shift( @_ );
my $timeout = shift( @_ );
eval {
local $SIG{ALRM} = sub {die "alarm"};
alarm $timeout;
$cmd_result = `$cmd`;
alarm 0;
};
if ($@) {
print STDERR "Command \"$cmd\" timed out.\n";
return undef;
} else {
return $cmd_result;
}
}
my $x = sys_alarm("sleep 10; echo foo", 5);
print "Output: " . $x . "\n\n";