hi,
I've been looking for a way to send an e-mail on start-up/shutdown of the pi.
I'm able to send a mail on boot by defining this in crontab but the mail sended always has the date 1/1/1970. If i run the script after boot the time/date is correct.
i'm not able to preform a task on shutdown.
Been looking a while on the internet but found nothing usefull.
Anyone a trick for this?
(using: Debian GNU/Linux 7.0 (wheezy))
-
- Posts: 12
- Joined: Tue Jan 22, 2013 6:03 pm
Re: Task at shutdown/startup
Somewhat historic, this thread. Looking for exact the same, I found a script which I had to modify. The result:
Make it executable. In this example it is named and located in ~/Scripts/SystemEmail. Don't forget to replace the e-mail address.
Copy or link it to /etc/init.d/:
It uses a lockfile, you have to make it on your own:
Now it has to be written to the runlevels. Because of the LSB header you can use update-rc.d
Now it is installed starting in runlevel 2, 3, 4 and 5 and stopping in 0, 1 and 6.
Runlevel 0 is halt, 1 is single user mode without network connectivity, 6 is reboot - - it sends a mail "System Shutdown".
Runlevel 2, 3, 4 and 5 it sends a mail "System Startup". I don't know the details about these runlevels, sometimes I read runlevel 2 is multiuser but without network connectivity, sometimes I read runlevel 2 is default in Debian.
Don't tested, yet - feel free to do it
Franz
Code: Select all
#!/bin/sh
# chkconfig: 2345 99 01
# Description: Sends an email at system start and shutdown
### 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: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#############################################
# #
# Send an email on system start/stop to #
# a user. #
# #
#############################################
EMAIL="mail@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
. /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}
Copy or link it to /etc/init.d/:
Code: Select all
sudo ln -s /home/pi/Scripts/SystemEmail /etc/init.d/SystemEmail
Code: Select all
sudo mkdir /var/lock/subsys
sudo touch /var/lock/subsys/SystemEmail
Code: Select all
sudo update-rc.d SystemEmail defaults
Runlevel 0 is halt, 1 is single user mode without network connectivity, 6 is reboot - - it sends a mail "System Shutdown".
Runlevel 2, 3, 4 and 5 it sends a mail "System Startup". I don't know the details about these runlevels, sometimes I read runlevel 2 is multiuser but without network connectivity, sometimes I read runlevel 2 is default in Debian.
Don't tested, yet - feel free to do it

Franz