Have your linux server email you when it gets rebooted

Did your linux server get accidentally or mysteriously rebooted? Did it come up after a crash?

You probably want to know about it as soon as possible so you can check on your database to make sure it’s okay.

If your linux server is configured to send mail, you can add a cron job to tell you when your server has been rebooted. Just add this entry to your crontab and substitute mail@example.com with your own email address:

@reboot /bin/sleep 120 && echo "*** $(hostname) has been rebooted $(date "+%h %d, %Y %H:%M %Z") ***" | /bin/mail -s "*** $(hostname) has been rebooted ***" mail@example.com

This cron job executes when the server comes back online. The cron entry above sleeps for 120 seconds before it sends an email to you. You can adjust the sleep time or remove it from the cron entry altogether:

@reboot echo "*** $(hostname) has been rebooted $(date "+%h %d, %Y %H:%M %Z") ***" | /bin/mail -s "Attention DBA team: $(hostname) has been rebooted" mail@example.com

@reboot is a non-standard crontab value that takes the place of the first 5 fields of the crontab entry (minute, hour, day of month, month, day of week).

Leave a Reply

Your email address will not be published. Required fields are marked *