Solved

Shell Script

Posted on 2013-12-10
7
274 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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