• In Chat
  • About
  • Contact
  • Browse

    Latest Coding

    Latest Daily

    Latest Health

    Featured Story

Coding 2018

| Begin | File Bank |  Register | Reply | Search | Statistics | Manual |
Local Coding / Local /  

Cron to check if server is running with email alert and restart

Author Admin

#1 | Posted: 7 Oct 2018 14:22 | Edited by: Admin 
I came across a script which was more or less perfect for the job. On implementing it though I had no end of trouble getting it to run under cron. It worked perfectly off the command line, which was irritating really as I could not get it to work with cron whatever I tried. I eventually came up with a working solution for MAMP which meant a great deal of the code was removed and a makeshift arrangement for the email alerts.

The initial script from nixCraft
My working version on MAMP

As I failed miserably to get the email alerts working (they weren't that reliable even off the command line) I created a file residing on my website which outputs a message to my email address on being pinged. The curl command does the same thing. I have found this type of email alert to be very reliable and fires every time. The code for the email file was this ...

$sender = 'mkjglobal@googlemail.com';
$recipient = 'mkjglobal@googlemail.com';

$subject = "Server Offline Warning";
$message = "Server has been restarted";
$headers = 'From:' . $sender;

if (mail($recipient, $subject, $message, $headers))
echo "Message accepted";

echo "Error: Message not accepted";

Which I found here: ARCLAB

I then created a server-offline-mail php file inside a folder on my website which I protected with the .htaccess file - only my home IP Address can gain access to it. Code for the .htaccess file ...

order deny,allow
deny from all
allow from

At the mac terminal I added the cron task by these steps ...

1. crontab -e

1 * * * * /Applications/MAMP/hq/monitorMysql.sh >/dev/null 2>&1

For testing purposes I left off the >/dev/null 2>&1 and as I have this code at the very top of my crontab -e screen ...


I get an email alert every time a cron runs. WITH the code at the end of the cron I no longer get the email alerts.

Should you want to set up a cron for a shorter period than every minute (cron limitation you can use plist which is a another way macs can set up recurring tasks). You can, of course, just run the above code endlessly and controlling how often it runs through sleep, but plist isn't that hard to setup and works well.

Handy website to work out cron intervals ...

How to use Plist instead of Cron to run scripts

Step 1: create a plist file such as the one below (it runs the file I created above). Alter it to suit ...

My working plist file for above with 30s refresh

To activate or deactivate use these commands ...

launchctl load /Applications/MAMP/hq/monitorMysql.plist
launchctl unload /Applications/MAMP/hq/monitorMysql.plist

To load automatically everytime your mac is started refer to this page ...

Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra?

I've tested the plist file I created above and it works fine.


As I house a very large, database driven shop on this server there is a tendency for the server to go offline. In time I will tweak my.cnf settings so this should occur less often. Not that it happens all that often now, but knowing my luck it will go offline in the early hours of the morning rendering all the scripts that rely on the database to fail. I now have a system in place where I can be alerted should the database be offline and to be restarted automatically. A big worry off my mind.
Website Designer

Your Reply Click this icon to move up to the quoted message

Only registered users are allowed to post here. Please, enter your username/password details upon posting a message, or register first.


Coding Powered by Forum Software miniBB ®
Online now: Guests - 1
Members - 0
Max. ever online: 33 [10 May 2019 12:44]
Guests - 33 / Members - 0
No Of Posts

  • Latest Daily Post ...

  • Latest Health Post ...


Phone: +01 0775 8107 427



email icon Mark Jones

Latest Story


  • Copyright © 2018 MKJ Brainwaving