I'm a lone web developer with my own Centos VPS hosting a few small web sites for my clients. Today I discovered my httpd service had stopped (for no apparent reason - but that's another thread). I restarted it but now I need to find a way that I can be notified by email and/or SMS if it happens again - I don't like it when my client rings me to tell me their web site doesn't work!
I know there are probably many different possibilities, including server monitoring software. I think all I really need is a script that I can run as a cron job from my dev host (which is permanently running in my office) that attempts to load a page from my production server and if it doesn't load within say 30 seconds then it sends me an email or SMS. I'm pretty rubbish at shell scripting, hence this question.
Any suggestions would be gratefully appreciated.
Well... The most simple script, I cam write:
Add it to cron as:
But it is too simple to tell you what the problem is if it exists.
UPD: Now this one-liner checks for a specific string on the page ("Normal operation string"), which should appear only on normal operation.
UPD2: A simple way to send the error page in the e-mail:
It's minus is that the page is re-requested in case of first test failure. This time the request may be successful and you won't see the error. Of course, it is possible to store the output and send it as an attachment, but it will make the script more complex.
Take a look at this script:
curl
is a command-line utility to fetch a URL. The script checks the exit code ($? refers to the exit code of the most recent command in a shell script) and if it was anything other than 0, reports an error (an exit code of 0 generally refers to success). As mentioned in HUB's answer, you can also just||
on the command-line to run a second command when the first one fails.Once you have the status figured out, you just have to send yourself some mail. Here is an example that uses the
mail
command to send mail from a shell script, assuming the box you're testing from has SMTP setup:BTW: if you're not good at shell scripting, don't limit yourself to a shell script. You could use a ruby script, a php script, any kind of script your server can run! Just add the
#!/path/to/executable
line at the beginning of the script - for instance:#!/usr/bin/php
Check this script. it's checking against a list of websites and sends email (to list of emails) whenever something wrong (http response different from 200). The script creates a .temp file to "remember" which website(s) failed at last check so you won't get multiple emails. the .temp file is deleted when the website is working again.
Add the following lines to crontab config ($ crontab -e)
Available on Github
I know that all the above scripts are exactly what you've asked, but I would suggest looking at monit because it will send you an email if apache is down but it will also restart it (if it's down).
I would recommend pingdom for this. Their free service allows you to check 1 site, but that's all you need to check 1 server. If you have an iPhone they push-message you for free, so no need to buy SMS credits from them, and they have multiple settings you can use. Mine is set to notify me after 2 retries (10min) and every 10min downtime after that. It's awesome, since it also checks for HTTP 500 messages indicating a site is down. If it fails, it immediately checks your site again from a different server in a different location. If that one fails, well, that triggers your preference in how/when you'd like to get notified.
Slight variation of the above.
A script to check if a website is available every 10 seconds. Log failed attempts into a
siteuptime.txt
file so it can be looked at (or graphed in excel) later.As you have many sites on your VPS, I would recommend You can open account with website monitoring site such as host-tracker.com. Apart from alerting you if site is down or not they also provide you weekly,monthly and yearly uptime of ur sites. Whish is very helpful for management and performance.
How about this: