One line cron entry to receive email of web server reboot
Posted Wednesday 13.08.08Jump down to
The preamble (read as “waffling”)
Web server reliability can be (and is) measured in a number of different ways. One of the most straight forward and easiest ways to get a quick idea of web host and server reliability is to look at a machine’s uptime, the amount of time since the machine last booted up.
Regular reboots are indicative of an unreliable server that requires regular reboots to rectify issues, or (and this is what I prefer to go with) demonstrate bad management, in that system administrators rather pull the plug and let the machine forget about its woes than actually investigate issues and fix them or put measures in place to prevent them happening again. This was something I discovered with my previous host, Dreamhost. I got the impression they concentrated too much on loading servers with as many users as possible and didn’t exercise enough (or any) quality control of what those users were in turn loading onto the servers themselves, and regularly resorted to pushing the reset button to cure issues.
On unix based systems, finding out the system’s current total uptime since last boot is a simple case of issuing the ‘uptime’ command that produces a line similar to:
09:36:50 up 158 days, 13:34, 1 user, load average: 0.22, 0.33, 0.34
(issuing ‘top’ offers the same info, plus further info on CPU usage, memory usage and running processes)
From the output it can quickly and easily be seen that in this case the web server has been up and running for 158 days, 13 hours and 34 minutes. There’s just one user logged into the machine (me) and it is under relatively low load given the 1, 5 and 15 minute load averages.
For your information, the above output was generated on my hosting companies server. Although this is a .co.uk website, the site is actually hosted through an Australian company on a server located in Australia. The company is GD Webhosting and the quality of service I have received from them since the start of this year has been excellent, both in terms of website availability and technical support.
But anyway, logging in everyday, even once a week to check system uptime can become a chore, and besides, who is to say that in between checks the system hasn’t been rebooted 50 times? Instant email notifications that aren’t generated as a result of a false-positive could also possibly be used as ammunition if having a whinge at the hosting company.
Although there are numerous (open source, free and paid) methods of monitoring system health, using tools such as RRDtool and Nagios to name but a couple, these can be more than a little complicated to install and configure by your regular non-superhuman-nerd-unix-geek. Aside from those there are also websites that offer to monitor your site’s and server’s availability, such as mon.itor.us and HostTracker, but checks are usually at best only every 30 minutes or so (unless you pay a premium) - enough time for a server to go down and come back up, losing site visitors but evading detection. Some hosting companies offering system health status update pages, but these usually only give you current state and are unlikely to tell you about the reboot at 3:30AM when you were fast asleep.
The solution
If you just want a simple email that tells you when your server has come back online, it can be done quite simply with a one line entry in your crontab:
@reboot echo `uptime` | mail -s "Server ABC rebooted" name@domain.com
(the back ticks around uptime are important)
When hosting with Dreamhost, they had the kindness to send me at least one email a week as a result of such an entry being added to my crontab, which reaffirmed my suspicions that the server, and therefore my websites, were going down fairly frequently, sometimes when I was in bed and therefore blissfully unaware of issues.
See the note below for why the output of uptime is being used as the e-mail’s body as opposed to any other plain message.
For details on how to edit your crontab if you are unsure, consult this how-to, your hosting control panel (cpanel offers a GUI for example) or your web host’s help area.
Caveat
One caveat you should be aware of, and this is something I have only recently realised. @reboot will fire if the cron daemon is restarted, which of course is not only limited to when the server is booted up. Normally this shouldn’t be much of an issue because once a server is up and running, crond stays running until the system is shutdown, but some problem installations may result in regular automatic or manual cron daemon restarts. False-positives may also occur when sys admins need to carry out maintainance.
For this reason, the output of uptime is piped into mail and used as the body of the notification email. This is so you know for sure whether or not the system has really just come back online.