Share this page: Follow me to be notified of new articles:

LastCall (simple cron job monitoring tool in PHP)

What is it?

A simple PHP script to monitor cron jobs. If a cron does not run properly, you will receive an email.
LastCall can also send you an email every X days (7 by default) to let you know it is still monitoring your cron job correctly.

What do I need?

  1. A server/PC with Linux
  2. PHP 4/5

What can I do with the source code?

What ever you want. (learn more)
Would you like to improve it? If you want, send me the updated version and if you want, I'll publish it here. (contact me)

How do I use it?

  1. On your server, create a file "lastcall.php" with the source code below
  2. Replace "YourEmailAddress@YourEmailDomain.com" with your email address in the script (twice)
  3. List all your cron jobs (and the normal delay between 2 jobs) under "List of cron jobs" in the script
  4. Add the notification source code at the end of all your cron jobs
  5. Create a new cron job on your server to run LastCall every minute
  6. Test if LastCall works correctly by removing the notification source code from one of your cron jobs

Changelog

Source code

Create a PHP file "lastcall.php" on your server with the following content:

Notification source code

At the end of all your cron jobs, add the following code:

<?php
// LastCall notification
$TmpFilesPath = "/tmp/"; // Path of temp files
$FilePrefix = "LC_"; // Prefix for the log files
$CronFileName = trim($_SERVER['SCRIPT_NAME'], "/"); // Cron file name (example: test.php)
if(strpos($CronFileName, "/")) { // In case of a complete path, we will remove the path
	$CronFileName = substr($CronFileName, strrpos($CronFileName, "/") + 1, strlen($CronFileName));
}
$FileToPing = $TmpFilesPath.$FilePrefix.$CronFileName.".txt"; // Final file path
touch($FileToPing); // Creation of the ping file
?>

Run LastCall with a cron job

We advise you to create a cron job that runs every minute to get LastCall notifications as soon as possible.

You can edit your crontab with the following command:
crontab -e

You just have to add the following line with the correct path to the script:
* * * * * php /path/to/script/lastcall.php

What happens when a cron job does not run?

When a cron job does not run, you will receive an email like this one :
Subject: [LastCall] PROBLEM cron my_cron_job_1.php
Hi there,
The cron -my_cron_job_1.php- is late.
The last ping we received was Wednesday 6th of August 2014 at 08:00:07 PM.
Have a great day!

Later, when the cron is working again, you will receive another email, for example:
Subject: [LastCall] RECOVERY cron my_cron_job_1.php
Hi there,
The cron -my_cron_job_1.php- is working again.
The last ping we received was Wednesday 6th of August 2014 at 08:08:08 AM.
The problem lasted for 8 minutes.
Have a great day!
Last update of this page: March 27, 2017
Please do not copy any of the content of this website without asking me first.