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 nixCraftMy 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 = 'firstname.lastname@example.org';
$recipient = 'email@example.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 ...
deny from all
allow from 192.168.1.300
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.