Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

DIY Server Uptime Monitor

There are many free (and commercial) uptime monitoring services that will alert you if your server goes offline. These services are popular because it’s mission critical to know when your sites are down. The sooner you know about it, the sooner you can scramble to get everything back online. I’ve tried other scripts and services but nothing that met my specific needs: simple, secure, lightweight and blazing fast. So decided roll my own DIY server status monitor and share it here at Perishable Press. Enjoy!

A barebones server uptime monitor written in PHP.

Contents

Requirements

The requirements for this app are simple:

Important! For best results, the uptime monitor app should be hosted on a separate server. If you host your websites and the uptime app on the same server, and the server goes down, the app won’t be able to run. So you won’t get an email alert.

Implementation

Here is an overview of how to implement DIY uptime monitor:

  1. Download and unzip the app folder
  2. Make a few edits in the app file
  3. Add the app folder to your server
  4. Add a cron job via your server

Done! Remember to test well. After satisfactory testing, this app “set it and forget it”, with no further action on your part. Once in place, the script will keep an eye on your sites and send an email alert if any site is down.

Let’s walk through each of the steps..

1) Download the app

Download the latest version. License is GPL version 2 or later.

Download DIY Server Status MonitorVersion 1.2 ( 1.44 KB ZIP )
If you enjoy this app, please donate to help ensure future development :)

2) Edit the app file

Unzip the download file to get the following folder/files:

/diy-server-status-monitor
			/.htaccess
			/check.php

Important: Make sure both files are included in the app folder. The .htaccess file is required because it forbids any public access to the app files. So the check.php file is protected from external abuse.

Next, open check.php. The first thing to edit is the array of site names/URLs. This is located in the first function, shapeSpace_urls(), as shown here:

function shapeSpace_urls() {
	
	return array(
		
		'Example Site Name' => 'https://example.com/',
		
	);
	
}

Replace https://example.com/ with your site’s URL. If you have multiple websites to monitor, you can add them like so:

function shapeSpace_urls() {
	
	return array(
		
		'Example Site 1' => 'https://example.com/',
		'Example Site 2' => 'https://domain.tld/',
		
	);
	
}

The last edit is to add your email address, so the script can send a notification if/when the site goes offline. To do this, scroll to the function shapeSpace_email() and look for this line:

$email = 'admin@example.com';

Replace the email address with your own. Save changes and done.

3) Upload the app folder

After editing the app file, upload the app folder to your server. The public root directory is recommended, but it’s fine to add to any public directory that you prefer. It’s also fine to change the app folder name diy-server-status-monitor as desired. Whatever folder/file names you end up using, remember to make a note of the file path, as it will be needed when setting up the cron job.

4) Set up a cron job

The final step is to add a cron job to run this script as frequently as desired. As mentioned, you want to add the cron job to an alternate server. It only makes sense to put the cron and app on a second server somewhere. If you put it on the same server as the site you are monitoring, it won’t run if the server goes offline.

Check out the next section for an example cron job.

Tip: Most web hosts provide server management software that makes it easy to create cron jobs. For example cPanel and Plesk both have screens where you can set up as many crons as you’d like. If unsure, ask your web host.

Example cron job

I don’t want to derail this tutorial with a deep dive into the exciting world of cron. But I do want to give you an example, so you have a starting point or reference when setting up your cron job. Here is the cron command that I use to ping my own sites’ monitoring script every 5 minutes:

*/5 * * * * usr/bin/php -q php /home/server/public_html/diy-server-status-monitor/check.php >/dev/null 2>&1

Note: cron is very specific in terms of file paths and such, so this example probably is not going to work “as-is” on your server. It’s just an example to give you a starting point. Again, ask your web host for help setting up cron if needed.

Let’s break down this example cron job..

The time interval

First, the */5 * * * * is cron syntax specifying that the job should run every five minutes. You can change this to any time interval.

The script command

The next part, usr/bin/php -q php /home/server/public_html/diy-server-status-monitor/check.php, is the actual command that we want to run every five minutes. It calls the PHP module and sends a request to the target file, check.php.

Effectively this command is what “pings” the uptime monitor app, so it can run and do it’s thing. Here it is critical that the file path be 100% correct. So if you modified the app file and/or folder name, this is where that comes into play.

Stoping cron spam

The last part of the cron job is >/dev/null 2>&1, which tells cron to redirect the output (stdout) to /dev/null. In plain language, it’s simply telling the server to not send an email report every time the command is run. This way, only the uptime monitor email alert will be sent, not all the cron output spam.

Or if you actually want a flood of cron spam, simply omit the >/dev/null 2>&1 from the command. Sometimes I do this for troubleshooting purposes, or when regular spam just isn’t enough.

Testing

Testing scripts called by cron can be tricky business. Some web host cron UIs (like Plesk) make it easy to run any cron job with a click. cPanel lacks this feature, sadly. That said, here are some ways to make testing a little easier.

Add a nonexistent test URL

When defining your websites in shapeSpace_urls(), you can add a domain/URL that does not exist, something like 29743skjdhf09273.tld:

'Nonexistent Test Site' => 'https://29743skjdhf09273.tld/',

When a nonexistent domain is added to the monitoring list, it will trigger an email alert. This makes it easier to tell if the script is working, because no alerts are sent for sites that exist and are online. Remember to remove the nonexistent site once testing is complete.

Send email for online and offline

Another way to do it, is to send an email alert for sites that are online. Normally only offline sites send an alert. So to always send an alert even if the site is online, locate the following code in the function, shapeSpace_status():

if (stripos($status, 'ONLINE') === false) {
	
	shapeSpace_email($status, $site);
	
}

Replace that bit of code with this:

shapeSpace_email($status, $site);

..so that it always sends an email alert regardless of server status. Then after testing is complete, reverse the procedure to restore the original, conditional functionality.

Related: Another cron script over at WP-Mix: Monitor File Changes via Cron.

Conclusion

I monitor the status of my own websites with this app. Been using it for a few months now and it works great. Very elegant and simple yet effective. Monitors uptime for any website, yours or otherwise ;)

About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
Blackhole Pro: Trap bad bots in a virtual black hole.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Thoughts
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Crazy that we’re almost halfway thru 2024.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.