It is always nice to have notification of when a server reboots, especially when you are on vacation, away from the office, or just lounging around! In order to send an email on reboot, we can easily create a cron job to run on reboot. Depending on your specific Linux distro, you may encounter some problems with this email going out in a timely manner!

First, in order to have a command run on boot we can either create a reboot cron job, or add a line to the /etc/rc.d/rc.local file. Note that the crontab @reboot job may only run when the machine is rebooted, not from a cold-boot (like after the power goes out).

To use the crontab @reboot option, start by editing your crontab file and adding a line like the following (edit your crontab file with the crontab -e command):

@reboot /root/

If you instead want a script that will run every time a server is booted, add the link to your script in the /etc/rc.d/rc.local file:

# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

Now, on reboot our server will run the /root/ script!

Next, we actually need to write the emailnotify script. This script will send us an email with some basic info when the server is started. The contents of my script are below:


sleep 60

#/bin/systemctl restart sendmail.service
/sbin/service sendmail restart

IP=`hostname -i`
HOSTNAME=`hostname -f`
echo "$HOSTNAME online.  IP address: $IP" > /root/email.txt
echo >> /root/email.txt
date >> /root/email.txt

mail -s "$HOSTNAME online" -r restart@server.domain.tld myemail@mydomain.tld < /root/email.txt
mail -s "$HOSTNAME online" -r restart@server.domain.tld myotheremail@myotherdomain.tld < /root/email.txt
mail -s "$HOSTNAME online" -r restart@server.domain.tld mycellphone@txt.carrier.tld < /root/email.txt

#cat /root/email.txt
rm -rf /root/email.txt

#/bin/systemctl restart sendmail.service
/sbin/service sendmail restart

Ok, so let's take a look at what this script is doing.
First, the bash script waits for 60 seconds, to give everything on the system ample time to startup. This isn't really necessary, but if the power goes back off within that 60 seconds, you won't get hit with multiple emails. This will also help keep our server from trying to send an email when the network equipment (switches/routers) hasn't fully recovered yet.

Next, we want to restart the sendmail service. This was very important on my Fedora 16 test machine. Without restarting sendmail, the email would get deferred and placed in a queue, and not actually be sent for a while. The actual error was in the /var/log/maillog file, and looked like this:

sendmail[1076]: pBMFxVSH001076: from=, size=607, class=0, nrcpts=1, msgid=<4ef353e3.ZS93/Yzasdfp1B4q%restart@server.domain.tld>, proto=ESMTP, daemon=MTA, relay=localhost []

Also notice that there are two different lines in the script to restart the sendmail service. The top line will work on newer Fedora systems, while the bottom line should work on any RPM based system, at least for the foreseeable future.

Now we will find the IP address and hostname of our server, and create our email message.
Finally we are going to send our email to three email addresses. I like to have one sent to my corporate email, one sent to my personal email, and another sent to my phone as a text message. This ensures that I get the message in a timely manner.

Finally we will delete our text file now that the email has been sent, and restart the sendmail service again just for good measure!