SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
How To Monitor Application Error Logs – Bash Script
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To......................................................................................................................................................1
Pre-requisites................................................................................................................................................1
Script – Snippet.............................................................................................................................................1
Email Notification..........................................................................................................................................4
E-Mail Notification – Subject ....................................................................................................................4
E-Mail Notification – Error Log..................................................................................................................4
How To Monitor Application Error Logs – Bash Script
1 | P a g e
Overview
The purpose of this script is to send notification to the email recipients application error logs file.
The script will scan and capture all the error(s) that have been triggered from current time -2 hours
onwards.
The log format and the script was tested and deployed was JBoss Application Server.
Applies To
Any Linux Operating system that supports bash shell
Pre-requisites
 Bash shell.
 Log Date Format : 2015-09-27 11:02:20
Script – Snippet
#!/bin/bash
SPLIT_LINE=$'n'
LOG_PATH=/usr/local/jboss/server/slend/log
LOG_FILE_NAME=${LOG_PATH}/server.log
APLN_NAME="S Application Deployed on `hostname`"
TO_LOG_FILE_NAME=ErrorLog.txt
ERROR_LOG_FILE_NAME=Error.txt
MAILTO=mailuser@domainname.com
CCMAILTO=ccmailuser@domainname.com
HRS=`date +%H`
HRS=`expr ${HRS} - 2`
#HRS=${HRS}`date +":"%M`
LOG_DATE=`date +%Y"-"%m"-"%d" "`
SEARCH_TIME=${LOG_DATE}${HRS}
# Verify Log File exists
#
if [ ! -e ${LOG_FILE_NAME} ]; then
echo "Log file ${LOG_FILE_NAME} Does't Exists !!!"
echo "${APLN_NAME} $SPLIT_LINE Log File ${LOG_FILE_NAME} File Does't Exists !!!" | mail -s
"${APLN_NAME} Error" $MAILTO
exit 1
fi
How To Monitor Application Error Logs – Bash Script
2 | P a g e
echo "Search Time is : " ${SEARCH_TIME}
#
# Set Hours to extract 12 hours
#
if [ "${HRS}" -eq "-1" ]; then
${HRS}=`expr "${HRS}" + 12`
SEARCH_TIME=${LOG_DATE}${HRS}
echo "-1 Setting Time Search : " ${SEARCH_TIME}
#
# Set Hours to extract 00 Hours
#
elif [ "${HRS}" -le "00" ]; then
${HRS}="${HRS}"
SEARCH_TIME=${LOG_DATE}${HRS}
echo "00 - Setting Time Search : " ${SEARCH_TIME}
#
# Set search to extract
#
elif [ "${HRS}" -eq "-2" ]; then
# `expr "${HRS}" + 12`
${HRS}=`expr "${HRS}" +2`
SEARCH_TIME=${LOG_DATE}${HRS}
echo "Setting Time Search : " ${SEARCH_TIME}
echo "-2 Setting Time Search : " ${SEARCH_TIME}
fi
if [ "${HRS}" -le "9" ]; then
HRS="0""${HRS}"
echo Now the Hours ::: ${HRS}
SEARCH_TIME=${LOG_DATE}${HRS}
fi
cd ${LOG_PATH} > /dev/null
pushd ${LOG_PATH} > /dev/null
echo Search Time : ${SEARCH_TIME}
#
# Find Matching Line number of the matching time in the log file
#
LINE_NO=`grep -n "${SEARCH_TIME}" ${LOG_FILE_NAME} | head -n 1 | cut -f1 -d :`
#
# If No matches were found
#
if [ -z ${LINE_NO} ]; then
How To Monitor Application Error Logs – Bash Script
3 | P a g e
echo "No Matches Found !!!"
echo ${LOG_FILE_NAME}
exit 1
fi
#
# Extract Log 2 lines before and 20 lines after ERROR
#
#LL=`wc -l ${LOG_FILE_NAME} | cut -f4 -d " "`
FILE_LINE_COUNT=`wc -l ${LOG_FILE_NAME} | awk '{ print $1}'`
FROM_LINE=`expr ${FILE_LINE_COUNT} - ${LINE_NO} + 5`
tail -n ${FROM_LINE} ${LOG_FILE_NAME} > ${TO_LOG_FILE_NAME}
grep "ERROR [" ${TO_LOG_FILE_NAME} -B2 -A20 > ${ERROR_LOG_FILE_NAME}
#
# If error Log file is empty.
#
if [ -e ${ERROR_LOG_FILE_NAME} ] && [ ! -z ${ERROR_LOG_FILE_NAME} ]; then
FILESIZE=`ls -l | grep ${ERROR_LOG_FILE_NAME} | awk '{ print $5 }'`
#
# If file size is zero byte
#
if [ "${FILESIZE}" = 0 ]; then
echo "Null File"
rm ${TO_LOG_FILE_NAME} ${ERROR_LOG_FILE_NAME}
exit 1
fi
#
# Send E-Mail Notification
#
echo ${ERROR_LOG_FILE_NAME}
ls -l
echo "Send Condition"
mail -s "${APLN_NAME} Error Log File" -c $CCMAILTO < ${ERROR_LOG_FILE_NAME} $MAILTO
echo "HRS : " ${HRS}
echo "Search Time : " ${SEARCH_TIME}
echo "Log Date Time : "${LOG_DATE}
echo "Line NO. :" ${LINE_NO}
echo "File Lines Count : " $FILE_LINE_COUNT
echo "File Line : " $FROM_LINE
#tail -n 1 ${ERROR_LOG_FILE_NAME} > COMPAREFILE
rm ${TO_LOG_FILE_NAME} ${ERROR_LOG_FILE_NAME}
fi
How To Monitor Application Error Logs – Bash Script
4 | P a g e
Email Notification
Upon running the shell script an email will be sent with the subject line set in the shell script.
E-Mail Notification – Subject
E-Mail Notification – Error Log
After each error a line separator will be inserted “--”
2 Lines before the “ERROR” and 20 Lines after the Error will be printed.

More Related Content

What's hot (10)

PDF
MindMap - Forensics Windows Registry Cheat Sheet
Juan F. Padilla
 
PDF
Laravel - The PHP Framework for Web Artisans
Windzoon Technologies
 
PDF
Oracle APEX 기초 워크샵 - 실습 가이드 문서: Part 1 (1/2)
Taewan Kim
 
PPT
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 
PPTX
A Deep Dive into Kafka Controller
confluent
 
PDF
Certidao inss
Jvilhena Martins
 
PDF
Sql server replication step by step
laonap166
 
PPTX
Parameter tampering
Dilan Warnakulasooriya
 
PDF
The RED Method: How to monitoring your microservices.
Grafana Labs
 
MindMap - Forensics Windows Registry Cheat Sheet
Juan F. Padilla
 
Laravel - The PHP Framework for Web Artisans
Windzoon Technologies
 
Oracle APEX 기초 워크샵 - 실습 가이드 문서: Part 1 (1/2)
Taewan Kim
 
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 
A Deep Dive into Kafka Controller
confluent
 
Certidao inss
Jvilhena Martins
 
Sql server replication step by step
laonap166
 
Parameter tampering
Dilan Warnakulasooriya
 
The RED Method: How to monitoring your microservices.
Grafana Labs
 

Viewers also liked (20)

PDF
How to Troubleshoot SELinux Audit2Allow unable to open (null)
VCP Muthukrishna
 
PDF
How To Manage Yum Packages - Group Packages
VCP Muthukrishna
 
PDF
How To Manage Yum History
VCP Muthukrishna
 
PDF
How To Manage Yum Cache
VCP Muthukrishna
 
PDF
How To Manage Yum Repositories
VCP Muthukrishna
 
PDF
How To List YUM Packages
VCP Muthukrishna
 
PDF
Install Active Directory PowerShell Module on Windows 10
VCP Muthukrishna
 
PDF
Shell Script Disk Usage Report and E-Mail Current Threshold Status
VCP Muthukrishna
 
PDF
How To Install and Configure AWS CLI for Windows
VCP Muthukrishna
 
PDF
How To Install and Configure Chrony on RHEL 7
VCP Muthukrishna
 
PDF
How To Yum Package Installation
VCP Muthukrishna
 
PDF
How To View Login History and Logout History on RHEL 7
VCP Muthukrishna
 
PDF
How To Install and Configure VSFTPD on RHEL 7 or CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
PDF
How to Install Configure and Use sysstat utils on RHEL 7
VCP Muthukrishna
 
PDF
How To Configure FirewallD on RHEL 7 or CentOS 7
VCP Muthukrishna
 
PDF
Installation CentOS 6.3
VCP Muthukrishna
 
PDF
File Space Usage Information and EMail Report - Shell Script
VCP Muthukrishna
 
PDF
DNF Failed To Open Cache
VCP Muthukrishna
 
PDF
Bash Script Disk Space Utilization Report and EMail
VCP Muthukrishna
 
How to Troubleshoot SELinux Audit2Allow unable to open (null)
VCP Muthukrishna
 
How To Manage Yum Packages - Group Packages
VCP Muthukrishna
 
How To Manage Yum History
VCP Muthukrishna
 
How To Manage Yum Cache
VCP Muthukrishna
 
How To Manage Yum Repositories
VCP Muthukrishna
 
How To List YUM Packages
VCP Muthukrishna
 
Install Active Directory PowerShell Module on Windows 10
VCP Muthukrishna
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
VCP Muthukrishna
 
How To Install and Configure AWS CLI for Windows
VCP Muthukrishna
 
How To Install and Configure Chrony on RHEL 7
VCP Muthukrishna
 
How To Yum Package Installation
VCP Muthukrishna
 
How To View Login History and Logout History on RHEL 7
VCP Muthukrishna
 
How To Install and Configure VSFTPD on RHEL 7 or CentOS 7
VCP Muthukrishna
 
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
How to Install Configure and Use sysstat utils on RHEL 7
VCP Muthukrishna
 
How To Configure FirewallD on RHEL 7 or CentOS 7
VCP Muthukrishna
 
Installation CentOS 6.3
VCP Muthukrishna
 
File Space Usage Information and EMail Report - Shell Script
VCP Muthukrishna
 
DNF Failed To Open Cache
VCP Muthukrishna
 
Bash Script Disk Space Utilization Report and EMail
VCP Muthukrishna
 
Ad

Similar to Bash Script - How To Monitor Application Error Logs and Send Notification (20)

PDF
How To Audit Server Login and Shutdown or Reboot Activity
VCP Muthukrishna
 
PDF
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Embarcadero Technologies
 
PDF
Zenoss Monitroing – zendmd Scripting Guide
VCP Muthukrishna
 
PPT
Making Pretty Charts in Splunk
Splunk
 
PDF
Splunk conf2014 - Lesser Known Commands in Splunk Search Processing Language ...
Splunk
 
PDF
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
PDF
Orangescrum Time Log Gold add-on User Manual
Orangescrum
 
PDF
How To Check file exists and Delete PowerShell
VCP Muthukrishna
 
PDF
Flex tutorial
Samer Elabd
 
PDF
Windows PowerShell Basics – How To Create powershell for loop
VCP Muthukrishna
 
PPTX
Bioinformatica p4-io
Prof. Wim Van Criekinge
 
DOCX
Embedding custom ActiveX controls into SAP GUI
Dr. Kerem Koseoglu
 
PPTX
Best Practices in Handling Performance Issues
Odoo
 
PPTX
Flink. Pure Streaming
Indizen Technologies
 
PDF
Labs_20210809.pdf
ssuserb4d806
 
PDF
Pandora FMS: Outlook Anywhere Plugin
Pandora FMS
 
PDF
Improving the performance of Odoo deployments
Odoo
 
PDF
Troubleshooting Tips and Tricks for Database 19c - EMEA Tour Oct 2019
Sandesh Rao
 
PPTX
Warewolf Tools and Connectors Part 2
Carol Vanden Bussche
 
PDF
Statistical computing project
RashmiSubrahmanya
 
How To Audit Server Login and Shutdown or Reboot Activity
VCP Muthukrishna
 
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Embarcadero Technologies
 
Zenoss Monitroing – zendmd Scripting Guide
VCP Muthukrishna
 
Making Pretty Charts in Splunk
Splunk
 
Splunk conf2014 - Lesser Known Commands in Splunk Search Processing Language ...
Splunk
 
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
Orangescrum Time Log Gold add-on User Manual
Orangescrum
 
How To Check file exists and Delete PowerShell
VCP Muthukrishna
 
Flex tutorial
Samer Elabd
 
Windows PowerShell Basics – How To Create powershell for loop
VCP Muthukrishna
 
Bioinformatica p4-io
Prof. Wim Van Criekinge
 
Embedding custom ActiveX controls into SAP GUI
Dr. Kerem Koseoglu
 
Best Practices in Handling Performance Issues
Odoo
 
Flink. Pure Streaming
Indizen Technologies
 
Labs_20210809.pdf
ssuserb4d806
 
Pandora FMS: Outlook Anywhere Plugin
Pandora FMS
 
Improving the performance of Odoo deployments
Odoo
 
Troubleshooting Tips and Tricks for Database 19c - EMEA Tour Oct 2019
Sandesh Rao
 
Warewolf Tools and Connectors Part 2
Carol Vanden Bussche
 
Statistical computing project
RashmiSubrahmanya
 
Ad

More from VCP Muthukrishna (20)

PDF
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
PDF
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
PDF
How To Connect To Active Directory PowerShell
VCP Muthukrishna
 
PDF
How To List Files on Remote Server - PowerShell
VCP Muthukrishna
 
PDF
How To List Files and Display In HTML Format
VCP Muthukrishna
 
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
PDF
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Open SSH Server on Ubuntu
VCP Muthukrishna
 
PDF
Windows PowerShell Basics - How To List PSDrive Info
VCP Muthukrishna
 
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
PDF
How To Construct IF and Else Conditional Statements
VCP Muthukrishna
 
PDF
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
PDF
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
PDF
How To Create PowerShell Function
VCP Muthukrishna
 
PDF
How To Disable IE Enhanced Security Windows PowerShell
VCP Muthukrishna
 
PDF
How To Check IE Enhanced Security Is Enabled Windows PowerShell
VCP Muthukrishna
 
PDF
How To Configure Nginx Load Balancer on CentOS 7
VCP Muthukrishna
 
PDF
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
PDF
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
How To Connect To Active Directory PowerShell
VCP Muthukrishna
 
How To List Files on Remote Server - PowerShell
VCP Muthukrishna
 
How To List Files and Display In HTML Format
VCP Muthukrishna
 
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
How To Install and Configure Open SSH Server on Ubuntu
VCP Muthukrishna
 
Windows PowerShell Basics - How To List PSDrive Info
VCP Muthukrishna
 
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
How To Construct IF and Else Conditional Statements
VCP Muthukrishna
 
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
How To Create PowerShell Function
VCP Muthukrishna
 
How To Disable IE Enhanced Security Windows PowerShell
VCP Muthukrishna
 
How To Check IE Enhanced Security Is Enabled Windows PowerShell
VCP Muthukrishna
 
How To Configure Nginx Load Balancer on CentOS 7
VCP Muthukrishna
 
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 

Recently uploaded (20)

PPTX
Practical Applications of AI in Local Government
OnBoard
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
Practical Applications of AI in Local Government
OnBoard
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
Next level data operations using Power Automate magic
Andries den Haan
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 

Bash Script - How To Monitor Application Error Logs and Send Notification

  • 1. How To Monitor Application Error Logs – Bash Script i | P a g e Table of Contents Overview.......................................................................................................................................................1 Applies To......................................................................................................................................................1 Pre-requisites................................................................................................................................................1 Script – Snippet.............................................................................................................................................1 Email Notification..........................................................................................................................................4 E-Mail Notification – Subject ....................................................................................................................4 E-Mail Notification – Error Log..................................................................................................................4
  • 2. How To Monitor Application Error Logs – Bash Script 1 | P a g e Overview The purpose of this script is to send notification to the email recipients application error logs file. The script will scan and capture all the error(s) that have been triggered from current time -2 hours onwards. The log format and the script was tested and deployed was JBoss Application Server. Applies To Any Linux Operating system that supports bash shell Pre-requisites  Bash shell.  Log Date Format : 2015-09-27 11:02:20 Script – Snippet #!/bin/bash SPLIT_LINE=$'n' LOG_PATH=/usr/local/jboss/server/slend/log LOG_FILE_NAME=${LOG_PATH}/server.log APLN_NAME="S Application Deployed on `hostname`" TO_LOG_FILE_NAME=ErrorLog.txt ERROR_LOG_FILE_NAME=Error.txt [email protected] [email protected] HRS=`date +%H` HRS=`expr ${HRS} - 2` #HRS=${HRS}`date +":"%M` LOG_DATE=`date +%Y"-"%m"-"%d" "` SEARCH_TIME=${LOG_DATE}${HRS} # Verify Log File exists # if [ ! -e ${LOG_FILE_NAME} ]; then echo "Log file ${LOG_FILE_NAME} Does't Exists !!!" echo "${APLN_NAME} $SPLIT_LINE Log File ${LOG_FILE_NAME} File Does't Exists !!!" | mail -s "${APLN_NAME} Error" $MAILTO exit 1 fi
  • 3. How To Monitor Application Error Logs – Bash Script 2 | P a g e echo "Search Time is : " ${SEARCH_TIME} # # Set Hours to extract 12 hours # if [ "${HRS}" -eq "-1" ]; then ${HRS}=`expr "${HRS}" + 12` SEARCH_TIME=${LOG_DATE}${HRS} echo "-1 Setting Time Search : " ${SEARCH_TIME} # # Set Hours to extract 00 Hours # elif [ "${HRS}" -le "00" ]; then ${HRS}="${HRS}" SEARCH_TIME=${LOG_DATE}${HRS} echo "00 - Setting Time Search : " ${SEARCH_TIME} # # Set search to extract # elif [ "${HRS}" -eq "-2" ]; then # `expr "${HRS}" + 12` ${HRS}=`expr "${HRS}" +2` SEARCH_TIME=${LOG_DATE}${HRS} echo "Setting Time Search : " ${SEARCH_TIME} echo "-2 Setting Time Search : " ${SEARCH_TIME} fi if [ "${HRS}" -le "9" ]; then HRS="0""${HRS}" echo Now the Hours ::: ${HRS} SEARCH_TIME=${LOG_DATE}${HRS} fi cd ${LOG_PATH} > /dev/null pushd ${LOG_PATH} > /dev/null echo Search Time : ${SEARCH_TIME} # # Find Matching Line number of the matching time in the log file # LINE_NO=`grep -n "${SEARCH_TIME}" ${LOG_FILE_NAME} | head -n 1 | cut -f1 -d :` # # If No matches were found # if [ -z ${LINE_NO} ]; then
  • 4. How To Monitor Application Error Logs – Bash Script 3 | P a g e echo "No Matches Found !!!" echo ${LOG_FILE_NAME} exit 1 fi # # Extract Log 2 lines before and 20 lines after ERROR # #LL=`wc -l ${LOG_FILE_NAME} | cut -f4 -d " "` FILE_LINE_COUNT=`wc -l ${LOG_FILE_NAME} | awk '{ print $1}'` FROM_LINE=`expr ${FILE_LINE_COUNT} - ${LINE_NO} + 5` tail -n ${FROM_LINE} ${LOG_FILE_NAME} > ${TO_LOG_FILE_NAME} grep "ERROR [" ${TO_LOG_FILE_NAME} -B2 -A20 > ${ERROR_LOG_FILE_NAME} # # If error Log file is empty. # if [ -e ${ERROR_LOG_FILE_NAME} ] && [ ! -z ${ERROR_LOG_FILE_NAME} ]; then FILESIZE=`ls -l | grep ${ERROR_LOG_FILE_NAME} | awk '{ print $5 }'` # # If file size is zero byte # if [ "${FILESIZE}" = 0 ]; then echo "Null File" rm ${TO_LOG_FILE_NAME} ${ERROR_LOG_FILE_NAME} exit 1 fi # # Send E-Mail Notification # echo ${ERROR_LOG_FILE_NAME} ls -l echo "Send Condition" mail -s "${APLN_NAME} Error Log File" -c $CCMAILTO < ${ERROR_LOG_FILE_NAME} $MAILTO echo "HRS : " ${HRS} echo "Search Time : " ${SEARCH_TIME} echo "Log Date Time : "${LOG_DATE} echo "Line NO. :" ${LINE_NO} echo "File Lines Count : " $FILE_LINE_COUNT echo "File Line : " $FROM_LINE #tail -n 1 ${ERROR_LOG_FILE_NAME} > COMPAREFILE rm ${TO_LOG_FILE_NAME} ${ERROR_LOG_FILE_NAME} fi
  • 5. How To Monitor Application Error Logs – Bash Script 4 | P a g e Email Notification Upon running the shell script an email will be sent with the subject line set in the shell script. E-Mail Notification – Subject E-Mail Notification – Error Log After each error a line separator will be inserted “--” 2 Lines before the “ERROR” and 20 Lines after the Error will be printed.