I wanted a method of automatically checking every so often if a site was up and running and came up with the following method that uses cron to run curl on an arbitrary page on the website. For example:
curl http://www.tuskdesign.com
Now in its default behaviour this will output the content of the page (i.e. the html) to standard out, or if there is an error to standard error. In both cases this is usually the terminal if you are using curl from the command line, or if it is in your crontab it will email you the output. But ideally we want to be emailed only if there is an error - having an email sent on success will mean that not only will you be inundated with emails if you run it regularly but you will have to open the email to see what the result was. So let's redirect the standard output to /dev/null so we only get an email if there is an error:
curl http://www.tuskdesign.com >/dev/null
Unfortunately this is not enough as if there is no output curl displays a progress meter which will still get emailed to you. Using the -s flag makes curl silent so that you get no ouput whether there is an error or not. This is also unsatisfactory as you will never get an email now in either case. However using the -s flag in conjuction with the -S flag solves that problem:
curl -sS http://www.tuskdesign.com >/dev/null
The -S flag is the 'show error' flag which will display any errors even in silent mode. So we have achieved our desired result. Now all we have to do is put this in our crontab with a line such as:
0 * * * * curl -sS http://www.tuskdesign.com >/dev/null
This will run the command at the beginning of every hour.
It is important to realise that an error in this case means that the site is unavailable for some reason. If you are getting a 404 status code for example (i.e. the site is up, but the page is unavailable), curl will interpret this as a success because something has been returned from the request. If you require more information than this such as when you get a response code other than 200, you will probably need to create a script of some kind to look in the return headers for the appropriate code.
Comments