Solved

Shell Script

Posted on 2013-12-10
7
272 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Fine Tune your automatic Updates for Ubuntu / Debian
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

830 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