Here's a simple fork/exec with an alarm. It won't cleanup multiple processes so it must use the >1 arg version of exec. I think you might already know that exec with more than 1 argument avoids using sh.
#!/usr/bin/env perl use warnings; use strict; my $ksecs = 5; my $csecs = 10; my $sig = 2; my $pid = fork; die "fork: $!" unless(defined $pid); if($pid == 0){ exec 'sleep', $csecs; # >1 arg exec to avoid using sh die "failed to spawn command: $!"; } local $SIG{'ALRM'} = sub { kill $sig, $pid }; alarm $ksecs; waitpid $pid, 0; # waitpid sets $? alarm 0; # turn off alarm in case child finished before parent if($? == $sig){ # we killed it (or someone else sent the same signal) }elsif($?){ # child had another error or signal }
In reply to Re: Killing a system/exec/`` call if it times out
by juster
in thread Killing a system/exec/`` call if it times out
by Cagao
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |