I thought I would thrown this out to the world incase anyone needs this in
the future. Might help those of us who "Google" though new
challenges... :-)<br/>
<br/>
Months ago I wrote a script that ran an external program and parsed the
output for errors. Unfortunatly, one of the errors that I did not
anticipate was the external program hanging forever. The usual<br/>
$result = `/path/to/cmd`;<br/>
hung and never returned -- thus the script never completed.<br/>
<br/>
I ended up writing this subroutine to call the command with a timeout
window defined.<br/>
example:<br/>
($TimeoutStatus, $CmdResult) =
timedexec(90,"/path/to/cmd");<br/>
This will call the program "command" and wait 90 seconds for it
to complete. The returned value in $TimeoutStatus will be 0(success)
or 1(failure). The value in $CmdResult will be the text output
returned by the command (to stdout).<br/>
<br/>
Dan<br/>
<br/>
sub timedexec {<br/>
# timedexec : This subroutine will run a command within a specified time<br/>
#
window (in seconds).<br/>
# RETURNS : Returns an array of two elements:<br/>
# Success : first element is 0<br/>
# second
element is the text returned by the command.<br/>
# Failure : first element is 1<br/>
# second
element is empty.<br/>
#<br/>
my $alarm=shift;<br/>
my $CMD=shift;<br/>
my $result = "";<br/>
my $status = 0;<br/>
<br/>
eval {<br/>
local $SIG{ALRM} = sub { die "alarm\n" }; # \n required<br/>
alarm $alarm;<br/>
$result = `$CMD`;<br/>
alarm 0;<br/>
};<br/>
die if $@ && $@ ne
"alarm\n"; # propagate
errors<br/>
if ($@) {<br/>
# timed out : return the error code.<br/>
$status=1;<br/>
$result="";<br/>
}<br/>
else {<br/>
# didn't time out : return a success code.<br/>
$status=0;<br/>
}<br/>
<br/>
return ($status, $result);<br/>
}<br/>
<br/>
<br/>
<br/>
- - - -<br/>
"Wait for that wisest of all counselors, time." -- Pericles<br/>
"I do not fear computers, I fear the lack of them." -- Isaac
Asimov<br/>
"Soon we will be able to harness the rotational energy from Orwell's
grave to solve all world energy problems." -- /. user GigsVT
(208848)<br/>
GPG fingerprint:6FFD DB94 7B96 0FD8 EADF 2EE0 B2B0 CC47 4FDE 9B68