Server startup alert [Scheduled jobs series #2]

This quick guide to sending automated alerts to you whenever your server or computer starts up should work for any Ubuntu distribution, and in fact most other linux distros.

We’ll create a nice simple script to send you a notification when your server starts up. Generally you’ll be the one turning on your server so there is arguably little use in this script but I still like it. My NAS is configured to boot automatically when the power is turned on (configure in your BIOS) so this script has been useful in letting me know when there has been a power cut at home as the script is triggered when the power returns.

You can make the script more complicated if you want too by adding additional functionality. At the bottom of this post I have included a more comprehensive boot script which also tests your drives and writes the results to a file. You can edit this basic script to include all of or parts of the other one and have your system email you the results.

A prerequisite to running this script successfully is having set up working email notifications in your distribution. See my guide here.

Let’s make a file to hold or script then open it to edit…

mkdir -p ~/scripts/startupemail
nano ~/scripts/startupemail/startupemail.sh

Add this as the content (replace your email address towards the end):

#!/bin/bash
# a script for sending myself an email on startup

echo "Server startup notification for:" > ~/scripts/startupemail/startupemail.txt
hostname >> ~/scripts/startupemail/startupemail.txt
echo " " >> ~/scripts/startupemail/startupemail.txt
echo "Startup occured at:" >> ~/scripts/startupemail/startupemail.txt
date >> ~/scripts/startupemail/startupemail.txt

#Send email
mutt -s "NAS Startup” your.email@address.com < ~/scripts/startupemail/startupemail.txt

Make executable:

chmod 755 ~/scripts/startupemail/startupemail.sh

Test:

sh ~/scripts/startupemail/startupemail.sh

Note that since I wrote this script I have edited it to send notifications via Pushbullet rather than email. See my quick and easy guide to setting up Pushbullet notifications here.

Add to startup via Webmin -> System -> Bootup and Shutdown -> Create New Upstart Service. Add executable file path to service.

Or add to cron to start at @reboot via Webmin or via command line using guide here.

As promised above, here is a link to a more complex boot script which tests your drive health. You’ll need to add a command to the end of it to send you the results if you decide to use this. Note that in my email guide linked above you can see an example of how to send attachments with your emails – so you can send your drive health results. Or you could be fancy and grep the results and only send notifications if there is an error.

Leave a Reply

(email optional)


Warning: Undefined array key "rerror" in /home/public/blog/wp-content/plugins/wp-recaptcha/recaptcha.php on line 291