sub runcmd { my @cmd = @_; defined( my $child_pid = fork() ) or die "fork: $!"; if ($child_pid) { #we are parent eval { local $SIG{ALRM} = sub {timeout($child_pid);}; alarm 5; waitpid $child_pid, 0; alarm 0; }; if ($@){print "oops: $@";} } else { #we are child exec @cmd or die "exec: $!"; } } sub timeout { my $pid = @_; kill 'TERM' => $pid; waitpid $pid, 0; die "reaped $pid\n"; }