Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

external command timeout 2

Status
Not open for further replies.

nychris

MIS
Dec 4, 2004
103
US
I have a large list of servers in @servers that I loop through and run a command on.

Code:
chomp(@out = `/some/command`);

Sometimes there will be something up with one of the servers and the command never finished which effectively hangs the script indefinitely. Is there a way to time this out after x amount of seconds?
 
look at the alarm function, it works great for those kind of issues..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Code:
eval {
  local $SIG{ALRM} = sub { die; }
  alarm(30); # 30 seconds
  system("/usr/bin/may-or-may-not-return");
  alarm(0); # turn off the alarm clock
};
if ($@) {
  warn "command timed out";
}

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
will $@ ever print because you are dieing in the sub?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
If an eval crashes (on purpose via die, on accident via dividing by zero etc), $@ is set to the error message ("Illegal division by zero at (eval 1) line X"... or "Alarm clock" in this case).

If eval exits successfully $@ is undef.

This is like Perl's version of try/catch. ;-)

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Er.. if you were asking about the "sub { die; }" part...

Even if it dies in a sub, a die would normally kill the entire program. Since the sub is inside the eval, the die doesn't kill the entire program but falls into the jurisdiction of the eval; it kills only the block of eval code and sets $@ to the error text.

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks.. that's what I was wondering about, I only use evals for the alarm function and I normally don't die there if the command fails but exit later after I clean up all my connections (db's, open files, shared memory, etc).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Kirsle, this is awesome and will be very helpful! I've been looking for a way to do this for some time. I have to loop through about 2000 servers and there's always one or two that will hang and kill the entire script. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top