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.
1.
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):
1.
@reboot echo "Server has restarted "`hostname` | mail -s "System Restart" example@example.com
Hit the escape button and then type
1.
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:
1.
nano SystemEmail
Paste the following code into that document and save it.
01.
#!/bin/sh
02.
# chkconfig: 2345 99 01
03.
# Description: Sends an email at system start and shutdown
04.
05.
#############################################
06.
# #
07.
# Send an email on system start/stop to #
08.
# a user. #
09.
# #
10.
#############################################
11.
12.
EMAIL="example@example.com"
13.
RESTARTSUBJECT="["`hostname`"] - System Startup"
14.
SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown"
15.
RESTARTBODY="This is an automated message to notify you that "`hostname`" started successfully.
16.
17.
Start up Date and Time: "`date`
18.
SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down.
19.
Shutdown Date and Time: "`date`
20.
LOCKFILE=/var/lock/subsys/SystemEmail
21.
RETVAL=0
22.
23.
# Source function library.
24.
. /etc/init.d/functions
25.
26.
stop()
27.
{
28.
echo -n $"Sending Shutdown Email: "
29.
echo "${SHUTDOWNBODY}" | mutt -s "${SHUTDOWNSUBJECT}" ${EMAIL}
30.
RETVAL=$?
31.
32.
if [ ${RETVAL} -eq 0 ]; then
33.
rm -f ${LOCKFILE}
34.
success
35.
else
36.
failure
37.
fi
38.
echo
39.
return ${RETVAL}
40.
}
41.
42.
start()
43.
{
44.
echo -n $"Sending Startup Email: "
45.
echo "${RESTARTBODY}" | mutt -s "${RESTARTSUBJECT}" ${EMAIL}
46.
RETVAL=$?
47.
48.
if [ ${RETVAL} -eq 0 ]; then
49.
touch ${LOCKFILE}
50.
success
51.
else
52.
failure
53.
fi
54.
echo
55.
return ${RETVAL}
56.
}
57.
58.
case $1 in
59.
stop)
60.
stop
61.
;;
62.
63.
start)
64.
start
65.
;;
66.
67.
*)
68.
69.
esac
70.
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:
1.
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.
1.
./SystemEmail start
1.
./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.
1.
cp SystemEmail /etc/init.d/
Last thing we do is set this up to run automatically by configuring it via chkconfig.
1.
chkconfig --levels 3 SystemEmail on
You will now receive two emails during a normal system reboot. Congradulations.