Help with shell script to send email once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with shell script to send email once
# 1  
Old 07-27-2011
Help with shell script to send email once

Hi Guys,
I have this script which will monitor oracle db process if up or down.And I want it to send email if it's down and the time it's back to online. However my script just keep on sending "Email Up" if the db is up or "Email Down" if the db is down.Is there any way to trap it so that it would only send Email Up or Down once?

This is script will not be run on cron jobs, I'm running it in background.
here is the sample script:
Code:
#!/bin/sh
while :
do
db_check=`ps -ef|grep ${ORACLE_SID}|grep pmon|wc -l`
status=`expr $db_check`

if [ $status -lt 1 ];
then
   echo "Down" > log.txt
else
   echo "Up" > log.txt
fi

log_check=`cat log.txt | grep Up`

if [ "$log_check" = "Up" ];then
        echo "Email Up"
    else
if [ "$log_check" = "Down" ];then
       echo "Email Down"  
    fi  
fi 
done

# 2  
Old 07-27-2011
You might want to add a sleep in such an interval as you'd like to have, lets say 60 for 1 minute, or 300 for 5 minutes, whatever.
# 3  
Old 07-27-2011
hi zaxxon,
If I will add a sleep its give's the same result. If I let it sleep for 1hour then every 1hour I will be receiving an email. :-( I have this following pseudo code in mind is this posible?

Code:
unli loop
check database status

if database status is up 
status=up

elif database status is down 

status=down
fi

# email checker
  if $status is up 
if email_status.txt is empty or email_status.txt= down
email the database is up
up > email_status.txt
fi

  elif $status is down
if email_status.txt is empty or email_status.txt= up
email the database is down
down > email_status.txt
fi
fi

end loop

# 4  
Old 07-27-2011
Ah ok, so you want to remember which last status it had. Yep you could just create a lock file, that keeps the latest status in it which will be tested against the actual status. Also I would set a lock to avoid running the script more than 1 times. If you stop it with a kill, you will have to manually remove the .lck file - maybe can put a trap in there for this.

I might write it for example like this:
Code:
#!/bin/bash
#
# Skript to check DB status and send an email as warning
#
# Error codes:
# 0     ok
# 1     already running or lock file left over
#
# v1.0, 27.07.11
###################################################################
#set -x

DATE=`date +%Y-%m-%d", "%H:%M:%S`
SNAME=`echo $0| sed 's/^.\///'`
BASE=/home/d3xt3r
LOCKDIR=${BASE}/var/.lock
LOCKFILE=${LOCKDIR}/${SNAME}.lck
STATFILE=${BASE}/var/DB_STAT

MAILTO=d3xt3r@bla.org
INTERVAL=10

if [[ -e $LOCKFILE ]]; then
    echo "Sorry, $SNAME seems to run already; lockfile $LOCKFILE already exists."
    exit 1
else
    touch $LOCKFILE
fi

mailit()
{
    echo "$DATE - $1"
    echo "$DATE - $1"| mail -s "Database status inside..."  $MAILTO
}

writestat()
{
    echo $1 > $2
}

getstat()
{
    #DB_STAT=`ps -ef| grep -v grep| grep ${ORACLE_SID}|grep -c pmon`
    DB_STAT=`ps -ef| grep -v grep| grep -c cron`
}

## Write initial status to file
getstat
case $DB_STAT in
    "0")    writestat DOWN $STATFILE;;
    "1")    writestat UP $STATFILE;;
esac

while :; do
    getstat
    if [[ $DB_STAT > 0 ]]; then
        STAT=UP
        if [[ `cat $STATFILE` == "DOWN" ]]; then
            writestat $STAT $STATFILE
            MSG="Database is $STAT again."
            mailit "$MSG"
        else
            echo "$DATE - Database is $STAT."
        fi
        sleep $INTERVAL
        continue
    else
        STAT=DOWN
        if [[ `cat $STATFILE` == "DOWN" ]]; then
            echo "$DATE - Database is still $STAT."
        else
            writestat $STAT $STATFILE
            MSG="Database is $STAT."
            mailit "$MSG"
        fi
        sleep $INTERVAL
        continue
    fi
done

exit 0

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 07-27-2011
hi zaxxon, thanks so much for the help it works!:-)
# 6  
Old 08-08-2011
hi zaxxon, finally i made it this script will send email once and does not resend it once it is done sending an email. I just want to share it with everybody.

Code:
#!/bin/bash
email_stat=stat.txt
mailbody=body.txt
mailto=dexter.com

echo "NONE" > $email_stat

while :
do

db_stat=`ps -ef|grep ${ORACLE_SID}|grep pmon|wc -l`

mail_stat=`cat stat.txt`
mail_stat="${mail_stat#"${mail_stat%%[![:space:]]*}"}"
mail_stat="${mail_stat#"${mail_stat##*[![:space:]]}"}"

if [ "$db_stat" == "1" ]; then
   if [ "$mail_stat" == "NONE" -o "$mail_stat" == "DOWN" ] 
   then
      echo "This is an email: The database is UP" > $mailbody
      echo "Do not reply on this email" | mail  -s "db status inside" $mailto < $mailbody
      echo "UP" > $email_stat
    fi
else
if [ "$mail_stat" == "NONE" -o "$mail_stat" == "UP" ] 
   then
    echo "This is an email: The database is DOWN" > $mailbody
    echo "Do not reply on this email" | mail  -s "db status inside" $mailto < $mailbody
    echo "DOWN" > $email_stat
    fi
fi
sleep 2
don

This User Gave Thanks to d3xt3r For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to read hive table and send email

HI Team, I am working on reading hive table and send email in email body using shell script, can you please help on fixing the errors: I have 6 columns in my hive table and trying to send the email in the mail body. below script: #!/bin/bash hive -e 'SELECT count(*) from db.table' >... (4 Replies)
Discussion started by: Mi4304
4 Replies

2. Shell Programming and Scripting

Need command to capture word from shell script and send email

Hello Experts, Greeting to all of you. I have a requirement, that we have a shell script status.sh that check the status of server and server status shows as status.sh Enterprise Server - Running Admin Server - Shutdown Requirement is like whenever the output shows shutdown it should... (2 Replies)
Discussion started by: aks_1902
2 Replies

3. Shell Programming and Scripting

how to send html email from shell script

This is my below script which is working fine as it send only plain text email. So My question is- How can I modify my below script to send this as an HTML formatted email? I want to show the text color of this expression `(echo "100*$TEST2/$TEST1" | bc -l)` as `RED` in an email and that can be... (3 Replies)
Discussion started by: raihan26
3 Replies

4. Shell Programming and Scripting

Shell script to send an email from the txt file

Hi Friends, Could you guys help me out of this problem... I need to send an email to all the users and the email has to be picked from the text file. text file contains the no. of records like: giridhar 224285 847333 giridhar276@gmail.com ramana 84849 33884 venkata.ramana@gmail.com... (6 Replies)
Discussion started by: giridhar276
6 Replies

5. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

6. Shell Programming and Scripting

How to send email through shell script

Hi All, I am new to the unix , i have to deliver one script very urgently I have to write a shell script where i have i want to send email to specific email id in this script i want FROM to be parameterized and stored in a variable TO to be parameterized and stored in a variable... (3 Replies)
Discussion started by: nileshbhawsar
3 Replies

7. Shell Programming and Scripting

How to send email once a day at certain time in unix shell script

hi, i have to send an email once a day at ceratin time say 22. i have tried with date commad, but not working.:( HOUROFTHEDAY=`date +'%H'` if ; then mailx -s "Info" emailid@org.com < $ProcessStatisticsFile fi Please help me... (5 Replies)
Discussion started by: sreelu
5 Replies

8. Shell Programming and Scripting

make un shell script to send email

Hi, Someone Knows how to obtein a chain from a unix file, but not all the line, for exemple, for this file ,obtein only 902111111 and to keep the value, this value will be used to make a mailx. NF=ALL, SUBJ= FROM: SN=CD, SE=TOPCALL, NA=, N=902111111, N=TCLFI TO: SE=FAX, NA=, C1=... (0 Replies)
Discussion started by: peybol
0 Replies

9. Shell Programming and Scripting

need help in finding a string and to send an email using shell script

Hi All i am writing a shell script which will search for a string "expires". once the search string is found it has to give the email address as the output and send an email to the person This is basically to find the encrypetd keys which are loaded in the unix server Below are sample... (10 Replies)
Discussion started by: ranga27
10 Replies

10. Shell Programming and Scripting

How to write a shell script to send an email to an id

Hi all, I want to know how to send an email using shell script ..... I tried a command call mail but it did not work.. Please any one reply.. With example if you know thanks.. Madhu (4 Replies)
Discussion started by: madhumathikv
4 Replies
Login or Register to Ask a Question