shutdown and restart

------------

Email on shutdown and restart (Linux)

# yum install -y mailx

Email Alert on Reboot-

# vim crontab -e

Add the following line..

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

a quick way to send an email every time his system (centos) stop, reboot or simply boot.

# vi /etc/init.d/SystemEmail

--------------------------------------------------------------------------------

#!/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}” | 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}

-----------------------------------------------------------------------------------

chmod u+x /etc/init.d/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.

/etc/init.d/SystemEmail start /etc/init.d/SystemEmail stop

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

chkconfig --levels 3 SystemEmail on

------