#!/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";