The ability to know when a system shutdown (gracefully) and when it came back up can be invaluable to a System Administrator. The tips below will send out an email when a Linux system is gracefully shutdown and again when the system has restarted. This means for a single reboot the Administrator will receive two emails. This solution will not send out an email if the system loses power, however an email will still be sent on reboot. This should indicate to the Administrator that something went wrong with the shutdown. The tips below are for a Red Hat based system but should be similar in other architectures.

Start Up Email (Easy Way)

First up is the blurb that will send an email on system start only. This is a simple crontab entry, using one of the special time specifications that cron provides.

crontab -e

Hit the ‘i’ button and on the first empty line paste the following to send an email to example@example.com on system start up (replace with your address):

@reboot  echo "Server has restarted "`hostname` | mail -s "System Restart" example@example.com

Hit the escape button and then type

wq

Now restart the system. After a few minutes you should recieve an email saying your system restarted successfully. Nice and easy, but it doesn’t quiet do what we want: send an email on start up AND shutdown. That brings us to:

Start Up and Shutdown Email (More advanced)

To get an email at both start up and shut down we need to write an init script. The tips below are specific to a Red Hat based system (Red Hat, Fedora, CentOS, etc) but should be fairly similar to others.

First thing we need is to create a new script (the example below uses nano, but vi, emacs or any other editor can be used to do the same thing). The following is the complete script used in this example – explanations will follow. In your home directory (or root’s home directory, at the end of this you want this script to be owned by root – this Tutorial does not cover permissions), type:

nano SystemEmail

Paste the following code into that document and save it.

#!/bin/sh
# chkconfig: 2345 99 01
# Description: Sends an email at system start and shutdown

#############################################
#                                           #
# Send an email on system start/stop to     #
# a user.                                   #
#                                           #
#############################################

EMAIL="example@example.com"
RESTARTSUBJECT="["`hostname`"] - System Startup"
SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown"
RESTARTBODY="This is an automated message to notify you that "`hostname`" started successfully.

Start up Date and Time: "`date`
SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down.
Shutdown Date and Time: "`date`
LOCKFILE=/var/lock/subsys/SystemEmail
RETVAL=0

# Source function library.
. /etc/init.d/functions

stop()
{
echo -n $"Sending Shutdown Email: "
echo "${SHUTDOWNBODY}" | mutt -s "${SHUTDOWNSUBJECT}" ${EMAIL}
RETVAL=$?

if [ ${RETVAL} -eq 0 ]; then
rm -f ${LOCKFILE}
success
else
failure
fi
echo
return ${RETVAL}
}

start()
{
echo -n $"Sending Startup Email: "
echo "${RESTARTBODY}" | mutt -s "${RESTARTSUBJECT}" ${EMAIL}
RETVAL=$?

if [ ${RETVAL} -eq 0 ]; then
touch ${LOCKFILE}
success
else
failure
fi
echo
return ${RETVAL}
}

case $1 in
stop)
stop
;;

start)
start
;;

*)

esac
exit ${RETVAL}

The main chunk of the code is the case statement at the bottom. When this script is set up, it will automatically be passed either “start” or “stop” as a parameter. Depending on that value, the case statement will either call the start() or stop() function. So, now that we have the script done, we need to set this up to run.

First, we need to make it executable:

chmod u+x SystemEmail

At this point you can test your code by running either of the two following commands. Both should send you an email, if you changed the appropriate variable in the script.

./SystemEmail start
./SystemEmail stop

Now we want to set this up to run at start up and shut down. Copy the script from your home directory to the init.d directory. Once it is here, you want the script to be owned by root. This will ensure that only the root user can make changes to the script. Since this will run everytime you start and stop your machine, this is a wise precaution.

cp SystemEmail /etc/init.d/

Last thing we do is set this up to run automatically by configuring it via chkconfig.

chkconfig --levels 3 SystemEmail on

You will now receive two emails during a normal system reboot. Congradulations.