Solved

Shell Script

Posted on 2013-12-10
7
262 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:CHAUDHARYAKSHAY
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now