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.
#1 by Ryan on July 27, 2009 - 3:21 am
Quote
This is very helpful! Thank you!
Ryan-Melbourne
#2 by Ulcervelt on March 9, 2010 - 4:38 am
Quote
thank!
#3 by Ulcervelt on March 17, 2010 - 9:05 am
Quote
Oh thank you!
#4 by Chris on March 8, 2010 - 1:43 pm
Quote
This is very useful, thank you!
One change you might want to make is to use the more standard ‘mail’ program, meant to send out e-mails like this, instead of ‘mutt’. I love mutt, but mutt’s not really for this purpose, and is not necessarily installed on every computer, though ‘mail’ would be more likely.
Also, it would be good if this script also sent out e-mails on reboot.
Thanks again!
#5 by Yoong on March 27, 2010 - 4:59 am
Quote
Hi,
Thanks for the tip, very useful.
Pb: I received only the email for the startup, not the shutdown. Any idea?
tx
Yoong
#6 by Alexey on April 8, 2010 - 3:02 am
Quote
You the best!
helpful for me
#7 by chriss on September 24, 2010 - 7:37 am
Quote
thanks, exactly what i was looking for
i had to install mailutils first on my ubuntu server but now it works on every reboot!
#8 by EEMS on October 15, 2010 - 4:56 pm
Quote
Thanks you for your work on this. For Ubuntu 10.04.1 Server (on a Thecus N5200Pro) I needed to make some changes to script file (copied below) because things are different places. I added the 3 sleep functions because the shutdown email was not making it out before the system shutdown and would then get sent on next reboot. I’m sure 2 of the 3 could be removed but after banging my head to get it to work I’m not going to mess around. The OS is on a flash DOM which might be to fast for the script to run?
This link was also where I got the bottom portion changes. Not sure if they make a difference:
http://www.linuxquestions.org/questions/linux-general-1/init-d-script-not-executing-at-shutdown-and-reboot-793154/
Also, instead of chkconfig I used the following:
sudo update-rc.d SystemEmail start 98 2 3 4 5 . stop 02 0 1 6 .
#!/bin/sh
### BEGIN INIT INFO
# Provides: SystemEmail
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Send email
# Description: Sends an email at system start and shutdown
### END INIT INFO
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/SystemEmail
RETVAL=0
# Source function library.
. /lib/lsb/init-functions
stop()
{
echo -n $”Sending Shutdown Email: ”
echo “${SHUTDOWNBODY}” | mail -s “${SHUTDOWNSUBJECT}” ${EMAIL}
sleep 4
RETVAL=$?
sleep 4
if [ ${RETVAL} -eq 0 ]; then
rm -f ${LOCKFILE}
sleep 4
success
else
failure
fi
echo
return ${RETVAL}
}
start()
{
echo -n $”Sending Startup Email: ”
echo “${RESTARTBODY}” | mail -s “${RESTARTSUBJECT}” ${EMAIL}
RETVAL=$?
if [ ${RETVAL} -eq 0 ]; then
touch ${LOCKFILE}
success
else
failure
fi
echo
return ${RETVAL}
}
case “$1″ in
start)
start
;;
stop)
stop
;;
status)
echo “Not applied to service”
;;
restart)
stop
start
;;
reload)
echo “Not applied to service”
;;
condrestart)
#
echo “Not applied to service”
;;
probe)
;;
*)
echo “Usage: SystemEmail{start|stop|status|reload|restart[|probe]”
exit 1
;;
esac
exit ${RETVAL}
#9 by EEMS on October 15, 2010 - 4:58 pm
Quote
I forgot, if it doesn’t work or changes need to be made to the script, run this to remove it:
sudo update-rc.d -f SystemEmail remove
#10 by Andy on October 19, 2010 - 12:56 pm
Quote
Thanks EEMS.
#11 by Andrea on January 21, 2011 - 3:32 am
Quote
Hi EEMS,
only one things:
whats happening if the @reboot task is run after the network is up?
The mail can’t send…
the script have to modify to run after that the network is up.
Any suggestion?
Andrea
#12 by EEMS on March 5, 2011 - 3:39 pm
Quote
Hi Andrea,
I never had a problem using reboot. I get one shutdown email and then one start up just as if I had shutdown and then powered back on. I was going to play around with trying to get a reboot email but for my home server that would be overkill. Sorry if I misunderstood the question.
EEMS
#13 by Pat O'Brien on March 9, 2011 - 3:52 pm
Quote
Hi, thank you for this! I am not getting notification when I do a shutdown -r for a reboot.
Do you know how to fix this?
I am on CentOS
#14 by Ryan Wilgoss on September 21, 2011 - 5:15 am
Quote
Hi all, first of all a great post, I also have the issue with not receiving the shutdown message. This is caused by the sendmail daemon not servicing the mail queue before it is shut down. I have cured this by introducing a sendmail -q in to the script thus;
_______________________________________________
echo “${SHUTDOWNBODY}” | mail -s “${SHUTDOWNSUBJECT}” ${EMAIL}
sendmail -q
_______________________________________________
Also I have added a 2 minute delay to the /etc/init.d/sendmail file thus;
_________________________________________________
stop() {
# Stop daemons.
sleep 120
echo -n $”Shutting down $prog: ”
killproc sendmail
_________________________________________________
This now works perfectly on quicker machines.
Hope this helps.
Ryan
#15 by Patrick on March 24, 2012 - 1:40 pm
Quote
Many many thanks for your post!
Pingback: » Linuxaria – Everything about GNU/Linux and Open source Email on shutdown and restart (Linux)
#16 by OM on May 31, 2012 - 3:26 am
Quote
You the best!
helpful for me
#17 by arturo on October 12, 2012 - 4:02 am
Quote
Thanks, brute force sleep idea helped a lot. Now it works.
I had the same problem. (That is sending a mail upon booting and shutdown. Paranoid notification…)
Thought the same approach.
Met the same problem: dns wasn’t ready when S99MailSender was executed.
Is there any way to tell a script to wait for dns ready?
# Required-Start: $remote_fs $syslog $network… etc.
Thanks,
arturo
#18 by udaya kumar k on November 5, 2012 - 12:13 pm
Quote
Hi All ,
i am new to linux and want to setup email notification on linux mint 12 (lisa )
while following first one i am getting
chkconfig –levels 3 SystemEmail onUnknown option: levels
usage:
chkconfig -A|–allservices (together with -l: show all services)
chkconfig -t|–terse [names] (shows the links)
chkconfig -e|–edit [names] (configure services)
chkconfig -s|–set [name state]… (configure services)
chkconfig -l|–list [--deps] [names] (shows the links)
chkconfig -c|–check name [state] (check state)
chkconfig -a|–add [names] (runs insserv)
chkconfig -d|–del [names] (runs insserv -r)
chkconfig -h|–help (print usage)
chkconfig -f|–force … (call insserv with -f)
chkconfig [name] same as chkconfig -t
chkconfig name state… same as chkconfig -s name state
And for
sudo update-rc.d SystemEmail start 98 2 3 4 5 . stop 02 0 1 6 .
update-rc.d: warning: /etc/init.d/SystemEmail missing LSB information
update-rc.d: see
Adding system startup for /etc/init.d/SystemEmail …
/etc/rc0.d/K02SystemEmail -> ../init.d/SystemEmail
/etc/rc1.d/K02SystemEmail -> ../init.d/SystemEmail
/etc/rc6.d/K02SystemEmail -> ../init.d/SystemEmail
/etc/rc2.d/S98SystemEmail -> ../init.d/SystemEmail
/etc/rc3.d/S98SystemEmail -> ../init.d/SystemEmail
/etc/rc4.d/S98SystemEmail -> ../init.d/SystemEmail
/etc/rc5.d/S98SystemEmail -> ../init.d/SystemEmail
while testing
sudo update-rc.d SystemEmail start 98 2 3 4 5 . stop 02 0 1 6 .
update-rc.d: warning: /etc/init.d/SystemEmail missing LSB information
update-rc.d: see
Adding system startup for /etc/init.d/SystemEmail …
/etc/rc0.d/K02SystemEmail -> ../init.d/SystemEmail
/etc/rc1.d/K02SystemEmail -> ../init.d/SystemEmail
/etc/rc6.d/K02SystemEmail -> ../init.d/SystemEmail
/etc/rc2.d/S98SystemEmail -> ../init.d/SystemEmail
/etc/rc3.d/S98SystemEmail -> ../init.d/SystemEmail
/etc/rc4.d/S98SystemEmail -> ../init.d/SystemEmail
/etc/rc5.d/S98SystemEmail -> ../init.d/SystemEmail
Could you please advise me .
Many thanks for your help.
Pingback: ubuntu: mengirimkan mail saat shutdown & restart « things left unsaid
#19 by heather lapthorne on April 20, 2013 - 3:33 pm
Quote
cant get back to my E-mails