Send Email on Shutdown and Restart Linux Server...



1- Install Services..


[ashutosh@server ~]# yum install mutt

[ashutosh@server ~]# yum install postfix



2- Create a new script ..

[ashutosh@server ~]# vim /etc/init.d/SystemEmail

________________________________________________________________________

#!/bin/sh

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

# Ashutosh

EMAIL="ashutosh.meritnation@gmail.com"

RESTARTSUBJECT="["`hostname`" "`date`"] – System Startup"
SHUTDOWNSUBJECT="["`hostname`" "`date`"] – System Shutdown"
RESTARTBODY="This is an automated message to notify you thatSaet No. NS-23 IP=10.0.3.166 "`hostname`" started successfully. Start up Date and Time: "`date`
SHUTDOWNBODY="This is an automated message to notify you thatSaet No. NS-23 IP=10.0.3.166 "`hostname`" is shutting down. Shutdown Date and Time: "`date`

LOCKFILE=/var/lock/SystemEmail
RETVAL=0
# Source function library.. /lib/lsb/init-functionsstop() {
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
log_success_msg
 else
 log_failure_msg
 fi
echo
return ${RETVAL}
 }
start()
{
 echo -n "Sending Startup Email: "
echo "${RESTARTBODY}" | mail -s "${RESTARTSUBJECT}" ${EMAIL}
 RETVAL=$?
if [ ${RETVAL} -eq 0 ]; then
 touch ${LOCKFILE}
log_success_msg
 else
 log_failure_msg
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}
________________________________________________________________________
 

3-  Make executable..

[ashutosh@server ~]# chmod u+x /etc/init.d/SystemEmail

4- Start Services..

[ashutosh@server ~]# service SystemEmail start
[ashutosh@server ~]# /etc/init.d/postfix restart


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

Alternative Simple Way for Email Alert on Reboot-
-------------------------------------------------------------------------------

[ashutosh@server ~]# vim crontab -e

Add the following line..

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

______________________________________________________________________________________
Click Back..                                Click Home..
https://docs.google.com/forms/d/1iNRZlJJO6rBFizzPcFmyOTEtfkdjhdVRmpM74IbiT3o/viewform