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:

#!/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

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.

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. :frowning: I have this following pseudo code in mind is this posible?

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

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:

#!/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
1 Like

hi zaxxon, thanks so much for the help it works!:slight_smile:

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.

#!/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
1 Like