LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-27-2014, 09:36 AM   #1
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Rep: Reputation: 33
little php script to check server reachable


Hello,

I have a little script to check if a webserver is reachable, and IF REACHABLE then write some data to database.

$db=mysql_connect("IP.AD.DR.ESS", "scriptuser", "passwd") or die("no connection");
if ($db) {
// do something
}

I execute this by issuing : /usr/bin/php mylittlescript.php

However, the script hangs when mysql_connect() can not connect. The script is not aborted (which should!)

Anyone have some approvements for me ?
 
Old 11-28-2014, 09:02 AM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
Quote:
Originally Posted by jonaskellens View Post
$db=mysql_connect("IP.AD.DR.ESS", "scriptuser", "passwd") or die("no connection");
if ($db) {
// do something
}
I would prefer something like this :

Code:
$db=mysql_connect("IP.AD.DR.ESS", "scriptuser", "passwd");
if (!$db) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($db);
 
Old 11-28-2014, 09:24 AM   #3
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
Hello,

thank you for your reply.

This does not help. It hangs on mysql_connect() if the MySQL-server is not reachable.
 
Old 11-28-2014, 09:33 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Hi

The default mysql connect timeout is 60. So you should get the message "no connection" after 60 seconds. Did you wait that long?

If you want it faster, you can set the timeout to for example 3 seconds like this:

PHP Code:
ini_set('mysql.connect_timeout',3); 
Do this before mysql_connect.
 
1 members found this post helpful.
Old 11-28-2014, 09:36 AM   #5
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
Thanks !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I check server load via php sir-lancealot Programming 3 03-04-2011 01:01 PM
server is not reachable girishankar Linux - Networking 2 08-28-2010 08:22 AM
script to check instance of PHP & mySQL running on Unix platform kushalkoolwal Programming 7 04-26-2006 10:40 PM
PHP script to check filetypes and put images into SQL table benrose111488 Programming 5 03-02-2005 02:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration