Re: script to mail@localhost when system reboots
- From: "danish" <me.linuxadmin@xxxxxxxxx>
- Date: 21 Apr 2006 11:43:28 -0700
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
.
- References:
- script to mail@localhost when system reboots
- From: danish
- Re: script to mail@localhost when system reboots
- From: Centurion
- Re: script to mail@localhost when system reboots
- From: Schraalhans Keukenmeester
- script to mail@localhost when system reboots
- Prev by Date: Re: script to mail@localhost when system reboots
- Next by Date: Re: How to fend off ng spam effectively...
- Previous by thread: Re: script to mail@localhost when system reboots
- Next by thread: Re: script to mail@localhost when system reboots
- Index(es):
Relevant Pages
|