Skip to content

Setting Up Pingman on Cron

Published: at 06:56 AM

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:

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:

Setting Up the Cron Job

There are two main ways to set up the cron job:

  1. 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.
  2. 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:

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.


Previous Post
PHP.ini Configuration
Next Post
.NET or C# Reference Links and Code Snippets