Skip to content

Instantly share code, notes, and snippets.

@karthikvee
Forked from fuzzmz/email.sh
Created June 6, 2016 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikvee/a1d6f26c63f3caedeafb9ae86997ac4e to your computer and use it in GitHub Desktop.
Save karthikvee/a1d6f26c63f3caedeafb9ae86997ac4e to your computer and use it in GitHub Desktop.
Script to send email on Debian based server at shutdown and restart.
#!/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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment