Solved

Shell Script

Posted on 2013-12-10
7
276 Views
Last Modified: 2013-12-19
I am using tail -f to monitor a log file that is being actively written to. When a certain string is written to the log file, I want to quit the monitoring, and continue with the rest of my script.

java.net.SocketException: Too many open files

java.net.SocketException: OutOfMemory
0
Comment
Question by:CHAUDHARYAKSHAY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39709671
Hi,

which is your OS - Linux or Windows?

On Linux, you could use "inotifywait" (the following is based on an example in the man page):

LOG="/path/to/logfile"
MSG1="java.net.SocketException: Too many open files"
MSG2="java.net.SocketException: OutOfMemory"
while inotifywait -e modify $LOG; do
  if [[ $(tail -n1 $LOG | grep -c  "$MSG1") -gt 0 || $(tail -n1 $LOG | grep -c  "$MSG2") -gt 0 ]]; 
then
    echo "Message found in $LOG"; break
  fi
done

Open in new window

0
 

Author Comment

by:CHAUDHARYAKSHAY
ID: 39709900
Hi, Thanks for such a quick response. The OS is Linux and I just wanted to restart my tomcat application whenever the given string is matched in the log file.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39709917
So add your restart command where I just put an echo "Message found in $LOG" (line 7).

If you want to have the script continue monitoring remove the "break" statement.

LOG="/path/to/logfile"
MSG1="java.net.SocketException: Too many open files"
MSG2="java.net.SocketException: OutOfMemory"
while inotifywait -e modify $LOG; do
  if [[ $(tail -n1 $LOG | grep -c  "$MSG1") -gt 0 || $(tail -n1 $LOG | grep -c  "$MSG2") -gt 0 ]]; 
then
    /etc/init.d/tomcat restart # or the like
  fi
done

Open in new window

0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

Author Comment

by:CHAUDHARYAKSHAY
ID: 39710005
okie. I got it. And how can I keep running this script in background continuously such that when the particular string is matched it restarts the application and still running in the background monitoring the log file for any future occurances.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39710030
nohup /path/to/scriptname >/path/to/scriptlog 2>&1 &

Note "scriptlog" is not the file you're monitoring, it just keeps a record of the script's output.
Further, the ampersand & at the end is not a typo, it is the "command" to make the script execute in the background.
0
 

Author Comment

by:CHAUDHARYAKSHAY
ID: 39727762
Hi, If I would like to send notification mail as well then where I need to inject it.

Also If there are multiple java applications running on the same box then do I need to setup this script for each application or is there a way that it can be done only by one script?
0
 

Author Comment

by:CHAUDHARYAKSHAY
ID: 39730727
If I would like to insert the code to kill the pid if it is present even after stopping the application then what needs to be done.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question