homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Perl to check if a website is running
...or if the server is down

 8:29 pm on Nov 6, 2003 (gmt 0)

I imagine there must be a way (I can't find it) of checking if a website is 'active', 'running' whatever you want to call it!

Recently the WorldPay payment system has been down, and I was wondering if I could check if WorldPay was up (using Perl) and if it is forward them to WorldPay. This is simple.

But if the server is not working, I don't want it to hang for 30 seconds or a minute with the customer waiting. Is it possible to request a timeout value?



 8:40 pm on Nov 6, 2003 (gmt 0)

use LWP::Simple qw(get);
if ( get("WORLD PAY URL GOES HERE") ) {
print 'ok';
} else {
print 'error';

But that way you perl script will hang and wait till WorldPay will respond. Timeout feature seems to be disabled in that module (at least for windows). I've tried it before with no luck.


 4:49 pm on Nov 10, 2003 (gmt 0)

You can always run a system command, such as ping.
Still, the timeout might be worth it...


 9:27 pm on Nov 13, 2003 (gmt 0)

This is just a snippet I took out of a server monitoring script we have, but you can probably get a start this way...

use Net::Ping;
use Time::HiRes qw( gettimeofday tv_interval);

$p = Net::Ping->new("tcp", 10);
$p->{port_num} = getservbyname($ref->{'PORT'}, "tcp");

$t0 = [gettimeofday];
$serverstate = "DOWN" unless $p->ping($ref->{'HOST'});
$pingtime = tv_interval($t0) unless ($serverstate eq "DOWN");


It's not the prettiest thing in the world, but it works reasonably well.

The docs for Net::Ping should get you on your way. For what you're doing, you might not bother with the Time::HiRes stuff.


 9:46 pm on Nov 13, 2003 (gmt 0)

Great, I give some of the ideas a try. Thanks


 6:22 am on Nov 18, 2003 (gmt 0)

Pinging the server box does not mean that HTTP server is running. It can be that the actual server is on and functioning, but HTTP server is down.

That happens very often as a matter of fact. Most of the load is on the HTTP server, not on the hardware or other servers (FTP, mail, etc...)


 8:44 am on Nov 18, 2003 (gmt 0)

You could use the Net::Telnet module if you want to be sure that the http server is running, rather than just the machine:

use Net::Telnet;
$connection=Net::Telnet->new(Timeout => 5, Host => "www.example.com", Port=>80, Errmode => sub {&error;});

sub error {
print "Connection Failed!\n";


 3:19 pm on Nov 18, 2003 (gmt 0)

The Net::Ping module does the same thing. Just use tcp instead of icmp and tell it to use port 80. I like it for it's flexibility.

I actually hadn't thought of using Net::Telnet that way.... hmmmm...


 9:35 pm on Nov 18, 2003 (gmt 0)

Many small hosting companies use nagios [nagios.org] (a sourceforge project) for monitoring their servers. I have never used it before but have read that it is a bit time consuming to setup with but is as good as paid servies like alertra.


 3:37 pm on Dec 3, 2003 (gmt 0)

We've just installed nagios. It's pretty good - ideal if you're running multiple web services (http, VOIP, SIP etc).



 10:04 pm on Dec 3, 2003 (gmt 0)

We use nagios as well and it has been quite good.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved