Web Hosting Talk







View Full Version : Perl & Timeouts


mainarea
08-27-2004, 11:45 AM
I'm using cacti & their ping script. The problem is, if one of the hosts is down, it just lingers for a long time. Here's the script:
#!/usr/bin/perl

$ping = `ping -c 1 $ARGV[0] | grep icmp_seq`;
$ping =~ m/(.*time=)(.*) (ms|usec)/;

print $2;

How can I add an argument so that if the script doesn't return a result within 5 seconds, it displays the result "0" and kills the ping process?

- Matt

Radchenko
08-27-2004, 02:51 PM
You'll have to use $SIG{ALRM} as follows:


eval { #Must put in eval otherwise the whole thing will die
local $SIG{ALRM} = sub { die "Ping timeout\n" };
alarm 5; #5 seconds
# Your ping command here
}
if ($@) {
print "Ping timed out\n";
} else {
# Print your ping results here
}


For more info you should take a look at "perldoc -f alarm"

SimplyDiff
08-27-2004, 04:41 PM
man ping

dawhb
08-27-2004, 08:06 PM
You can specify a timeout with the -w option.

man ping is a good thing to do :)