Apr 042012
 

For a customer i was in need of a quick way to send an email every time his system (centos) stop, reboot or simply boot.
I’ve found an useful article http://www.syntaxtechnology.com and most of this article is based on that.

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, depending on the boot system you’ll have to change slightly the script.



As first thing we need to create a new script. The following is the complete script used in this example – explanations will follow.
So in a terminal as root type:

vi /etc/init.d/mailme

As alternative use nano or emacs, the important is that you paste the following script in the new window:

#!/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="[email protected]"
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}" | mail -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}" | mail -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 /etc/init.d/mailme

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.

/etc/init.d/mailme start
/etc/init.d/mailme stop

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

chkconfig --levels 3 mailme on
Enhanced by Zemanta

Popular Posts:

Flattr this!

  9 Responses to “Email on shutdown and restart (Linux)”

  1. Thank you.

    This is very good work,

  2. Thanks, working for me on CentOS. However, in your article you save the file as “emailme” and in the rest of the article you reference to it via “mailme”. And, on our CentOS, I had to install bash mail by entering yum install mails.

    Other than that, userful.

  3. #!/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=”[email protected]
    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}” | mail -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}” | mail -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}

  4. Running CentOS 6 on a Dell R720. Tried this script with no luck. I noticed there is no SystemEmail in /var/lock/subsys/ directory. Do I copy the SystemEmail file I generated into that directory as well? Do I need to add a crontab entry? thanks!

  5. Thanks for sharing.
    It works as we expected.

    Joe

  6. Thanks for this useful script. Will this work for reboot of server as well? Because I tried rebooting the server but did not get a mail. I got mail when the server booted back.

  7. It’s not working for me. I am getting error

    Error msg : /etc/init.d/mailme: 13: .: Can’t open /etc/init.d/functions

  8. Script is working fine . Thanks you .

    Good work.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*