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:
- I cannot execute shell script periodically
- 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