in reply to Timeout on system command

I recommend using a piping form of open, and capturing the returned PID. Then, in your signal handler, you can kill the PID, to ensure it goes away. Example:

my $pid; eval { local $SIG{ALRM} = sub { die "TIMEOUT" }; alarm 2; $pid = open my $fh, "sleep 30|"; alarm 0; } if ($@ eq "TIMEOUT") { print "kill $pid\n"; kill 1, $pid; } elsif ($@) { die $@; }

Take note that your example code had an error -- the key in %SIG should be ALRM and not ALARM.

Update: apparently Joost posted essentially the same answer, but 10 minutes earlier than I did. Oh well, I guess that can be seen as validation of some sort.

Update: fixed silly syntax error