Error details in mail

Hi,

I have a application that writes log details to a file in a folder.
I am trying to write script to send mail to the user whenever the log is appended with "Error" string and details.
The user should receive error and error details in mail whenever there is a error in the file.

The application runs 24X7. It will continuously run and appends log to the file.

File Name: default.log
sample data in default.log
ERROR: Unable to connect to DB. Please check DB
ERROR: Unable to execute the script. There is some problem.

Please help.

Thanks,
Maddy

tail -f

will wait for lines to be added to a file.

#!/bin/ksh
cd /path/to/logs   # change to YOUR path to default .log
tail -f default.log | while read rec        # loop forever
do
   grep -q "ERROR"  "$rec"   # quiet option for grep
   if [ $? -eq 0 ] ; then
         echo "$rec" | /usr/bin/mailx -s "Error `date`"  maddy@mycompany.com
   fi
done

Each new occurrence of the word ERROR creates an new email.

1 Like

Hi,

I used this code, but it's not sending the sending the email.

Code:

#!/bin/ksh
cd /path/to/logs
# change to YOUR path to default .log
tail -f default.log | while read rec        # loop forever
do
    grep -q "INFO"  "$rec"
    # quiet option for grep
    if [ $? -eq 0 ] ; then
          echo "$rec" | /usr/bin/mailx -s "Error `date`"  maddy@mycompany.com
    fi
done

It's printing the as in the screen, with no mail.

Please help me out.

2nd argument of grep is a file name.
Either

printf "%s\n" "$rec" | grep -q "INFO"

or replace it by a case statement

case $rec in
 *INFO*) echo "$rec" | /usr/bin/mailx -s "Error `date`" maddy@mycompany.com
 ;;
esac

---------- Post updated at 02:25 PM ---------- Previous update was at 02:22 PM ----------

PS. Maddy, your code is hard to read. Please re-edit and wrap in CODE tags!

1 Like
#!/bin/ksh
RECIPIENTS="email"
grep "ERROR" filename in which error is there
if [ $? -eq 0 ]; then
echo "ERROR FOUND"
mailx -s "ERROR FOUND IN DataBase on `date | cut -c 5-10`" $RECIPIENTS < filename in which error is there
else
exit 0
fi