Re: script to mail@localhost when system reboots




Schraalhans Keukenmeester wrote:
Centurion wrote:
danish wrote:


Hi Everyone,
I have to create a script which sends a mail everyttime the system
reboots so I used the /var/log/wtmp file to gather the information when
the system reboots..But what I have written only works for reboots
which have already taken place. Can a script be developed which can
send mails to the system administrator whenever the system is going for
rebooting...

Thanks
Danish


You've been given plenty of "food for thought" but here's an actual working
script :) Some important comments at the bottom so read on.

---- bounce.sh ----
#!/bin/bash

# Script: bounce.sh
# Purpose: Sends basic information to admins immediately before shutdown or
# reboot and sends another message when the system comes back up.
# Author: James Gray
# Genesis: 20-April-2006
# Version: 0.1
# License: GPL (use and abuse at your pleasure - usual disclaimers apply)

#
# Define some variables and functions
#
TO=mail@localhost # edit as required
HOST=$(hostname)
umask 077
PATH=/bin:/sbin:/usr/bin:/usr/sbin
TMP=/tmp/bounce.txt
rm -f $TMP
touch $TMP

write(){
echo "$@" >> $TMP
}

mylevel(){
CUR=`runlevel | cut -c 3`
case $CUR in
0) echo "Shutdown"
;;
1) echo "Single User"
;;
[2-5]) echo "Multiuser"
;;
6) echo "Reboot"
;;
*) echo "WTF?"
;;
esac
}

#
# Begin the work here
#
LEVEL=`mylevel`
case $1 in
start) # System is coming up from a "down" state
SUBJ="$HOST - System entering $LEVEL Mode"
write "This is an automated message from: $HOST"
write "System came back up: $(date)"
;;
stop) # System is going down for shutdown/reboot
SUBJ="$HOST - System going down for $LEVEL"
write "This is an automated message from: $HOST"
write "System going down: $(date)"
write "System uptime: $(uptime)"
;;
*) # Not valid option - dump an error
echo "Useage: $0 [start|stop]"
exit 1
;;
esac

# Now send the mail
mail -s "$SUBJ" $TO < $TMP

exit 0
---- bounce.sh ----

Now simply make it executeable, copy it to /etc/init.d/bounce.sh and link it
in the relevant runlevels:
/etc/rc0.d/K20bounce.sh -> ../init.d/bounce.sh
/etc/rc6.d/K20bounce.sh -> ../init.d/bounce.sh
/etc/rc1.d/S99bounce.sh -> ../init.d/bounce.sh
/etc/rc2.d/S99bounce.sh -> ../init.d/bounce.sh
/etc/rc3.d/S99bounce.sh -> ../init.d/bounce.sh
/etc/rc4.d/S99bounce.sh -> ../init.d/bounce.sh
/etc/rc5.d/S99bounce.sh -> ../init.d/bounce.sh

You may need to edit the runlevel 0 and 6 "K" numbers so that this script
runs BEFORE the mail and network processes are stopped.

You'll only need to edit the "TO" variable and you're done. I've tested
this script on Ubuntu and RHEL4 and it works :) You may want to know why
it sends a message on boot - simple, sometimes I shutdown a system and
DON'T want it brought back up for one reason or another. At least with
this script I can see when/if the system was brought up. Then I go find
the engineer responsible and staple their tongue to an exhaust pipe and
drag them naked over broken glass :)

HTH,

James

crontab @reboot

Thank you all for continuing this post.......thank you for sharing the
knowledge.....
James ill be trying out your script, but my system in the office is
being used by my seniors for some database testing si I cant reboot
it.......

same is for Schraalhans Keukenmeester..and everybody else

Danish

.



Relevant Pages

  • Re: Help needed with a js command
    ... The original script allowed to shutdown any computer in the ... computer, and I just need to reboot one computer, anyway. ... Const EWX_SHUTDOWN = 1 ...
    (microsoft.public.scripting.jscript)
  • Re: Script to reboot group of PCs
    ... TRY: Shutdown /? ... /m = specify machine to shutdown ... >> Where machines.txt is a list of machine names or IP's you want to reboot. ... >> Use of any included script samples are subject to the terms specified at ...
    (microsoft.public.windows.server.scripting)
  • Re: shutdown/reboot suggestion
    ... I have such a script, I put it in /bin/require_hostname and symlinked ... shutdown, halt, reboot, fastboot, and fasthalt to this script: ... have inadvertently shutdown or rebooted the wrong machine. ... These are all distinct from my term window color scheme for my local host. ...
    (freebsd-questions)
  • Re: WMI and Joining Domain (sysprep related)
    ... > Just keep in mind that you have to rename the computer and reboot before you ... > use a script to automaticaly generate a sysprep.inf file that includes ... Dim sps_barcode,Flag,ExtraMsg ... '' Get workstation BIOS info for oeminfo '' ...
    (microsoft.public.scripting.vbscript)
  • RE: make world : did it fail?
    ... I know from the script output that the buildworld and buildkernel succeeded. ... The only step that is suspect is the installkernel. ... It reported the unpatched kernel. ... When I heard the computer reboot (it's sitting ...
    (freebsd-questions)