Monitor log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Monitor log file
# 1  
Old 05-10-2012
Monitor log file

Hi,

I need to amend an existing ksh script so that it runs a process (stop weblogic) and in parallel needs to monitor a log file (startup.log) in the background for a certain string (e.g. unable to stop weblogic). If the string appears in the log i need to kill the stop weblogic process.

Currently the script just runs the stop weblogic process and hangs if a certain string appears in the log file.
# 2  
Old 05-10-2012
How about running stop in background and monitoring the log+child-process in the foreground like this:
(assumption is you have /proc filesystem, if not might need to call ps instead of checking the /proc/$CHILD directory).

Code:
stop weblogin &
CHILD=$!
while ! grep -q "unable to stop weblogin" startup.log && [ -d /proc/$CHILD ]
do
   sleep 5
done
grep -q "unable to stop weblogin" startup.log && [ -d /proc/$CHILD ] && kill $CHILD


You could also add a saftey check, eg if after X loops it kills the child anyway (for taking too long).
# 3  
Old 05-11-2012
I will give this a try. Many thanks Chubler_XL!

---------- Post updated at 11:07 AM ---------- Previous update was at 08:27 AM ----------

Ok, this is a little more complicated then i thought.

Unfortunately i can't just grep the startup.log for the string "unable to stop weblogin", i need to tail the startup.log everytime my script is run as i need to ignore any previous instances of this string in the log.

Therefore i need to able to run 2 processes simultaneously

Process 1) Tail the startup.log for "unable to stop weblogin"
Process 2) Stop weblogic

and If process 1 finds the string before process 2 has completed I need it to kill process 2

...and if process 2 completes with process having found the string i need my script to kill process 1.

Any help would be much appreciated.
# 4  
Old 05-11-2012
Could you age the log such that the current log only applies to the current instance. This would be normal practice.
# 5  
Old 05-11-2012
That would definately make sense but unfortunately i am unable to change that process. The startup.log is archived per start/stop and therefore i have no choice but to 'tail -f' it.
# 6  
Old 05-13-2012
You could use awk to only look for new occurances of string within log:

(first line creates logfile if none exists - this simplifies rest of code as we can ignore file not existing case).

Code:
[ -f logfile ] || > logfile
LN=$(wc -l < logfile)
stop weblogin &
CHILD=$!
 
while [ -d /proc/$CHILD ] && awk "NR>$LN && /unable to stop weblogin/{exit 1}" logfile
do
   sleep 1
done
[ -d /proc/$CHILD ] && ! awk "NR>$LN && /unable to stop weblogin/{exit 1}" logfile && kill $CHILD


Last edited by Chubler_XL; 05-13-2012 at 08:50 PM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. Shell Programming and Scripting

Monitor and capture the latest entry from the log file

Hi, I want to monitor a log file using tail -f command and search for a specific string on the most recent entry from the file. If the search string matches with the most recent or last line from the file, I want send an email to the people with the message. tail -f service.log|tail -n 1 ... (5 Replies)
Discussion started by: svajhala
5 Replies

3. Shell Programming and Scripting

Script to monitor log file

Hi, Have written a script to monitor linux non standard log file based on line numbers, so each check store $otalinenum .. then in next check after 10 minutes it compre the current_total_line_num > last_total_line_num then it will parse the log file from last_total_line_num to... (0 Replies)
Discussion started by: Shirishlnx
0 Replies

4. Shell Programming and Scripting

Monitor log file for a Error and generate the e-mail.

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (1 Reply)
Discussion started by: hishanms
1 Replies

5. HP-UX

Script to monitor /var/opt/resmon/log/event.log file

AM in need of some plugin/script that can monitor HP-UX file "/var/opt/resmon/log/event.log" . Have written a scrip in sh shell that is working fine for syslog.log and mail.log as having standard format, have interrogated that to Nagios and is working as I required . But same script failed to... (3 Replies)
Discussion started by: Shirishlnx
3 Replies

6. Shell Programming and Scripting

Continiously monitor the log file

Hi Friends, I am trying to write a script which continiously monitor one specific error message from a log file. This script should continiously monitor the file for the error and send out the email when detect the error message. I tried the below command but fails. Please help me. tail -f... (4 Replies)
Discussion started by: arumon
4 Replies

7. Shell Programming and Scripting

Monitor dynamic log file for a particular Error

Hi Folks, I need help in creating a script to monitor a continuously updating log for one particular error. If the the script finds the error it should send out an email. Thanks for all ur help (2 Replies)
Discussion started by: a12ka4
2 Replies

8. Shell Programming and Scripting

Script to monitor the pattern in the log file

hi All, how to find a pattern in the log file & display the above and below line for example in the log file, i have many lines, whenever i search for "Category" it should display the above line with only few parameter like i want only the location name & department name Thu Jul 02 11:05:23... (2 Replies)
Discussion started by: rithick256
2 Replies

9. Shell Programming and Scripting

Monitor log file and execute command

I would like to monitor a log file using a shell script and as soon as a line with a certain string in it appears I would like to run a program. I have been playing around with doing this using tail -f, but cannot get it to work. I found something similar here:... (1 Reply)
Discussion started by: danielsbrewer
1 Replies

10. Shell Programming and Scripting

Bash tail monitor log file

Hi there, I have a problem here that involves bash script since I was noob in that field. Recently, I have to monitor data involve in logs so I just run command tail -f for the monitoring. The logs was generate every hour so I need to quickly change my logs every time the new hour hits according... (2 Replies)
Discussion started by: kriezo
2 Replies
Login or Register to Ask a Question