0

I want to scan a log file for JDBC and JMS exception and send an e-mail if such error is found.

Issue here with tail are:

  1. I cannot execute shell script periodically
  2. If someone kills tail command invoked from my shell script then I will no longer receive e-mail alerts.

So far I have developed below script:

#!/bin/bash
#This shell script monitors application logs file and sends alert email in case of JDBC or JMS error.

export LOGFILE=/usr/app/$USER/data/logs/dummyapp.log
export EMAILRECIPIENTLIST="opsteamdl@company.com"

#-------------------------------------------------------------------------
#----------- DO NOT EDIT AFTER THIS LINE ---------------------------------
#-------------------------------------------------------------------------
echo "Scanning log file - $LOGFILE"
tail -f $LOGFILE|while read line;
do
echo $line
if [ `echo $line|grep JDBCConnectionException|wc -l` -ne 0 ]; then
        mailx -s "[URGENT] - JDBCConnectionException reported in log" $EMAILRECIPIENTLIST < echo $line;
else if [ `echo $line|grep javax.jms.JMSException|wc -l` -ne 0 ]; then
        mailx -s "[URGENT] - javax.jms.JMSException reported in log" $EMAILRECIPIENTLIST < echo $line;
fi
fi
done
exit
Ranjeet
  • 151
  • 6
  • 18
  • what do you mean by _I cannot execute shell script periodically_, there are tools like `crontab`, `watch`,`at` to have your command run as per your requirement! – User123 Jan 23 '20 at 06:45
  • It will cause multiple tail commands to run in background which will be difficult to justify during audit. – Ranjeet Jan 23 '20 at 07:48
  • [Quick-and-dirty way to ensure only one instance of a shell script is running at a time](https://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at?r=SearchResults&s=1|183.9455) – David C. Rankin Jan 23 '20 at 07:52

1 Answers1

0

Your script looks fine, just few refinements:

tail -f $LOGFILE|while read line;
do
echo "$line"
if $(echo $line|grep -Eq "JDBCConnectionException")
then
    echo "$line" | mailx -s "[URGENT] - JDBCConnectionException reported in log" $EMAILRECIPIENTLIST && pkill -P $$ tail
elif $(echo $line|grep -Eq "javax.jms.JMSException")
 then
    echo "$line" | mailx -s "[URGENT] - javax.jms.JMSException reported in log" $EMAILRECIPIENTLIST && pkill -P $$ tail
fi
done

======================================================

pkill -P $$ tail: to kill the tail command started by your script. If you want your script to be running , you can exclude this part.

User123
  • 1,498
  • 2
  • 12
  • 26