Hi there,
I was trying to get a script working that will fire me an email when my database cannot be connected to. This is what I have and I'm wondering if it looks right to you php experts. :)

$conn = mysql_connect ($server,$user,$pass) or die(mail("$SendToEmail", "$yoursubject", $emailtext . mysql_error(), "From: $email"));


Also, does anyone know how to have an email sent when the appache services stop running (i.e. website unavailable). I'm trying to track how many times my isp's services fail. I plan on setting up a cronjob that will run the above line every 5 minutes along with one that checks to see if my site is still accessible.

Thank you for your time and brain power!

Dani AI

Generated

Good idea to get notified on failures, but a couple of practical points will make alerts reliable and useful.

The one-liner die(mail(...)) is brittle: it halts execution and prints whatever mail() returns (usually 1 or nothing), and it assumes the server MTA is available. was right that an external monitor is the most reliable way to detect a fully down site. ’s pointer toward PHP error handling is also relevant — centralizing error/exception handling gives you one place to log and notify.

Use explicit error handling for the DB connect, log the problem, then send an alert. Example using PDO with exceptions (safer and compatible with modern PHP):

<?php
$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 5];
try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
    $msg = "DB connect failed: " . $e->getMessage();
    error_log($msg); // persistent record
    mail($adminEmail, "DB connection failure", $msg, "From: monitor@yourdomain.com");
    exit(1);
}
?>

For detecting Apache/site outages, run checks from outside the host (a VPS, another trusted server, or a third-party service). A simple cron-friendly shell check:

#!/bin/sh
status=$(curl -s -o /dev/null -w "%{http_code}" "https://example.com/")
if [ "$status" -ne 200 ]; then
  echo "Site returned $status at $(date)" | mail -s "Site down" admin@domain.com
fi

Practical cautions: don’t spam yourself on flapping outages — implement rate limits or grouping, keep a timestamped outage log for counting failures, and prefer authenticated SMTP libraries (PHPMailer or similar) if your host’s mail() is unreliable. Note: the old mysql_* extension used in early threads was removed from PHP 7.0; use PDO or mysqli in modern code.

Recommended Answers

All 4 Replies

There are some services to check and email you if your website goes down. A partial list: http://www.webhostingtalk.com/showthread.php?s=&threadid=400382

Also, your PHP code looks right. Have you tried testing it? You can test it by changing your MySQL database info, for instance, so that it cannot connect.

Thank you for the resources. I've checked out the site monitoring services and signed up for a couple of them already. Are any of you familiar with CPanel? I guess that my ultimate goal is to create a script similar to the site monitor that is bundled with CPanel. I'm just not familiar enough with PHP and Apache to know where all of the services and variables are located to tell me when, for example, mail is down or apache itself has stopped running. Are there any other places you might be able to send me to help out with finding a way to monitor those services as well?

Thank you for the resources. I've checked out the site monitoring services and signed up for a couple of them already. Are any of you familiar with CPanel? I guess that my ultimate goal is to create a script similar to the site monitor that is bundled with CPanel. I'm just not familiar enough with PHP and Apache to know where all of the services and variables are located to tell me when, for example, mail is down or apache itself has stopped running. Are there any other places you might be able to send me to help out with finding a way to monitor those services as well?

If you run a site monitor script on your website to monitor the website, then when the website is down, you won't know, and neither will the script - since the script goes down when the website does! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.