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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timing out a check is it takes xx seconds

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello,

trying to figure out how to timeout/wait xx seconds for a command to run/return before it dying out and just printing command took to long etc. Not an external command in this case I am running a simple -w to check if two different directories are writeable. The problem is that something the device/mount goes offline and when the -w check is done it just sits there forever.

I found a few examples with SIG ALARM (which I don't quite follow) but not sure if there is a better/simpler way to do this.

example simple reduced code.
Code:
if ( (!-w $dirPath) && (!-w $dirPath2) ) { #how can I have  this check timeout after 5 seconds if no response comes back to -w?)
      do whatever
}

Thanks for the help in advanced.
 
maybe you check -e before you check -w?


Here's a very simple example stolen from a few posts down. Written by Kirlse
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";}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top