sub cmd_wrapper { my $timeout = shift; my $cmd = shift; my( $out, $rc ); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; $out = (`$cmd`)[0]; # Get first line of output $rc = $?; alarm 0; }; if( $@ ) { die "Eval failed for $cmd for unknown/unexpected reasons" if $@ ne "alarm\n"; die "Eval failed for $cmd because alarm timed out"; } die "Return code undefined for $cmd" unless defined $rc; return $rc, $out if wantarray; return $rc; } my $rc = cmd_wrapper(300, $pull_cmd); # Will timeout in 5 minutes (300 seconds) # Add your logging here