The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace all string instances found by find+grep umen Shell Programming and Scripting 0 12-06-2007 03:52 AM
Checking the header and trailer for a given string and if not found, exit out of the er_ashu UNIX for Dummies Questions & Answers 2 11-08-2007 09:55 AM
Search for string and display those NOT found John Rihn Shell Programming and Scripting 17 05-22-2007 08:22 AM
Display only found string Cranie Shell Programming and Scripting 5 01-21-2007 07:01 PM
Recursive Search and replace only when found string umen Shell Programming and Scripting 1 05-07-2006 01:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-11-2008
jake657 jake657 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Stopping tail -f when certain string found?

Hi,

I need to monitor a log file for a certain string ("Phase 2 ended") which indicates that the job which creates the log file has finished loading its data file. Once the string "Phase 2 ended" is found in the log file I would then like to stop checking that log and check to see if another logfile has been started - and start the whole process over.

Here's what I have so far ...
Code:
#!/bin/ksh
 
# Get name of the newest logfile
LogFile=`ls -tr /path/logfilename_* | tail -1`
 
if grep 'Phase 2 ended' $LogFile
then
  echo ----------
  echo "File has finished processing. No file currently being loaded."
  echo ----------
else 
  echo 
  echo ----------
  echo "File still being loaded. Will monitor progress, watch for \"Phase 2 ended\" message."
  echo ----------

  #Show all 'Phase' messages in log file so far...
  grep 'Phase' $LogFile

  #Keep checking so 'Phase 2 ended' message can be seen...
  tail -f $VAULogFile | grep Phase
fi
Now, of course, the above does not exit when it reaches 'Phase 2 ended' or the end of the file - it just sits there, in the way tail -f does.

I think I need a while read, so I can stop the tail -f and then loop to the next log file (if one yet exists) but I don't know how to do it.

Could one of you experts please point me in the right direction?


Many thanks


--
Jake
  #2 (permalink)  
Old 09-11-2008
Chr15 Chr15 is offline
Registered User
  
 

Join Date: Apr 2006
Location: UK
Posts: 5
You need to grab the PID of the tail proc and kill it

if string_found
get PID && kill $PID

and then loop round

Sorry no code but hope that points you in a direction that helps.
  #3 (permalink)  
Old 09-11-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Try:

Code:
  tail -f $VAULogFile | awk '/Phase/;/Phase 2 ended/ { exit }'
No need for a kill, the tail should terminate automatically when the pipe is closed.
  #4 (permalink)  
Old 09-12-2008
jake657 jake657 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Quote:
Originally Posted by Annihilannic View Post
Try:

Code:
  tail -f $VAULogFile | awk '/Phase/;/Phase 2 ended/ { exit }'
No need for a kill, the tail should terminate automatically when the pipe is closed.
That worked like a charm. Thank you very much, sir.



Thanks also, Chr15. I didn't really want to resort to a kill if possible but I appreciate your help. Thanks.
Closed Thread

Bookmarks

Tags
looping, tail -f, while read

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0