-   Linux - Newbie (
-   -   send eamil when computer restarts (

tmz 10-24-2002 06:44 AM

send eamil when computer restarts

I've just set up my linux. Now I would like that it notifies me, when it restarts.
Can you give me a hint or tell me some links where I could read about this?

Thank you!


unSpawn 10-24-2002 08:38 AM

Well, first you would like to get aquainted with the difference in runlevels (man init). Then you've got to decide if the box is able to (still) send an alert at that stage (network up).
Then you gotta write the script that will allow you to send the alert. If you reboot all applications are sent the TERM signal, and within a predefined timespan the KILL signal if they don't respond. This means your window for sending the message is small. You can compensate by running sendq when rebooted.
On a SYSV system you'd link the script from /etc/rc.d/init.d to /etc/rc.d/rc6.d/S01<scriptname> so it'll be the first one accessed.
Here's an example, untested so YMMV:
# Check runlevel
if [ "$(/sbin/runlevel | awk '{print $2}')" != "6" ]; then exit 1; fi
# Check networking still is up
ifconfig eth0 | grep -qe "UP"; net_up="$?"
if [ "$net_up" != "0" ; then exit 1; fi
# Send msg, only Subject necessary
/bin/true | mail -s "Reboot at $(/bin/date)" <address@domain>
# Exit with the exitcode of our last action
exit $?

tmz 10-24-2002 09:54 AM


If I understand correctly, you suggest to send email befor computer goes down. I wouldn't go so far. I just need to be mailed when the system is up again. When every service is up again, the system should send me email "I'm up again".

Do I have to write this <<script file>> in C++ and compile it, or is there an easy way.

Thank you!


acid_kewpie 10-24-2002 01:38 PM

you don't write script files in c++, c++ is a programming langauge, not a scripting language. the example script that unspawn gave nearly does what you asked, to convert it to send the mail when you load, you should place it in etc/rc.d/rc3.d/S99<scriptname> (or 5 if you boot to X) instead of rc6.d, and also change what runlevel you are checking for (i.e. 3 or 5 instead of 6).

you might prefer a simpler approach however (sending a mail on reboot is slightly more complex...) you can just add a simple line such as:

/bin/true | mail -s "Boot at $(/bin/date)" <address@domain>

to the end of a start up script such as /etc/rc.local but naturally that is less rigorous than the previous example, and not as elegant either.

tmz 10-28-2002 06:51 AM

Thank you. That's what I need.

All times are GMT -5. The time now is 08:22 PM.