Continuously Monitor Your Server Uptime
Maintaining server uptime is crucial for any web application or service. Downtime can lead to lost revenue, frustrated users, and reputational damage. By utilizing Pingman with cron jobs, you can proactively monitor your server’s health and receive timely alerts in case of outages.
This guide walks you through the process of setting up a cron job to automate the execution of the Pingman PHP script. This allows you to achieve continuous monitoring of your server’s uptime without manual intervention, ensuring you stay informed about any potential downtime issues.
The Pingman Script
<?php
function ping_log($server, $port, $timeout, $db, $email, $keep_alive_page_path) {
$current_status = ping($server, $port, $timeout);
if (file_exists($db)):
$previous_status = file_get_contents($db, true);
else:
file_put_contents($db, "up" + time());
$previous_status = "up";
endif;
if ($current_status == "down"):
file_put_contents($db, "down");
if ($previous_status == "down"):
mail($email, "Server down:" + $server, "Your server is down.");
endif;
else:
//keep alive here
keep_alive($server, $port, $keep_alive_page_path);
file_put_contents($db, "up" + time());
if ($previous_status == "down"):
mail($email, "Server back up:" + $server, "Your server is back up.");
endif;
endif;
}
function ping($host, $port, $timeout){
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}
function keep_alive($server, $port, $keep_alive_page_path){
$page = file_get_contents('http://'.$server.':'.$port.'/'.$keep_alive_page_path);
}
// SAMPLE
ping_log("example.com", "80", 10, "example_status.txt", "example@example.com", "example.com/keep_alive");
?>
Here is an explanation of the Pingman PHP script.
Prerequisites
Before diving into the setup, it’s important to have a few things in place:
- Pingman Script: Ensure you have downloaded and saved the Pingman PHP script on your server at the desired location.
- Basic Cron Job Knowledge: Having a basic understanding of cron jobs will be helpful for configuration. You can find numerous resources online to learn more about cron syntax and scheduling.
Running Pingman with Cron
To schedule the Pingman script for automatic execution using cron, we need to specify the correct php.ini
file to ensure proper configuration. Here’s the breakdown of the cron command to achieve this:
php -c /path/to/your/custom/php.ini /path/to/your/pingman/index.php >> /path/to/your/cron_error.log
Explanation of the Command:
php
: This invokes the PHP command line interface (CLI).-c /path/to/your/custom/php.ini
: This flag tells PHP to utilize a specificphp.ini
configuration file, ensuring the script runs with the appropriate settings. Replace/path/to/your/custom/php.ini
with the actual location of your customphp.ini
file./path/to/your/pingman/index.php
: This specifies the path to the Pingman script’s main file (index.php
). Adjust the path to reflect the location on your server.>> /path/to/your/cron_error.log
: This part redirects any output (including errors) to a log file namedcron_error.log
. This log file can help you identify any issues that might occur during script execution. Replace/path/to/your/cron_error.log
with your desired location for the log file.
Setting Up the Cron Job
There are two main ways to set up the cron job:
- Using your Hosting Control Panel: Most hosting providers offer a control panel like cPanel where you can manage cron jobs through a user-friendly interface.
- Manually Editing the Crontab File: For advanced users or specific server setups, you might need to directly edit the crontab file. This typically requires accessing your server through SSH and editing the crontab using a text editor.
Explanation of the Schedule:
*/5
: This indicates that the cron job will run every 5 minutes. You can adjust this value to suit your monitoring needs (e.g., every minute, every hour).* * * * *
: This represents the cron schedule syntax for “every minute, every hour, every day of the month, every month, every day of the week.” You can modify these values to define a custom schedule.
With this cron job in place, the Pingman script will be executed every 5 minutes. The output and any errors will be logged in the cron_error.log
file.
Importance of Cron Job Logs
The cron logs located at the specified path (/path/to/your/cron_error.log
) are crucial for monitoring/debugging purposes. These logs provide insights into any issues that might occur during the Pingman script’s execution.