Send me an email on computer shutdown

I would like the computer to send me an email everytime it starts or it shuts down. I found this code written for CENTOS and am trying to adapt it to Ubuntu.

The code is:

#!/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=""
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.
. /lib/lsb/init-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}

Where the /lib/lsb/init-functions is different from the original code to be compatible with Ubuntu.

The error I'm getting trying to run this is (the name of the file is mailme)

Unit mailme.service failed to load: No such file or directory

What can I do for this code to run on Ubuntu? Are the modifications large?

1

1 Answer

From your error message, it seems you're running a version of Ubuntu that uses systemd. I think it would be easier if you separated out the mail-sending part into a separate script and execute it using a systemd service.

So, taking out the mailing part (and simplifying it):

#! /bin/sh
EMAIL=""
SUBJECT="[$HOSTNAME] - System $1"
if [ "$1" = startup ]
then ACTION="started successfully"
else ACTION="is shutting down"
fi
# a printf format string to simplify a long body
BODY="This is an automated message to notify you that %s %s.\nDate and Time: %s\n"
printf "$BODY" "$HOSTNAME" "$ACTION" "$(date)" | mail -s "${SUBJECT}" "${EMAIL}"

Save this as, say, /usr/local/bin/bootmail.sh, make it executable, etc.

Then, to create a systemd service, create a file in /etc/systemd/system with extension .service (for example, /etc/systemd/system/bootmail.service) containing:

[Unit]
Description=Run Scripts at Start and Stop
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/bootmail.sh startup
ExecStop=/usr/local/bin/bootmail.sh shutdown
[Install]
WantedBy=multi-user.target

Now, do:

systemctl daemon-reload
systemctl enable bootmail.service

Now, you should get mails on startup and shutdown (assuming mailing is configured correctly, etc.).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like