Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell Script

Posted on 2013-12-10
7
Medium Priority
?
278 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 2000 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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 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