Stopping tail -f when certain string found?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stopping tail -f when certain string found?
# 1  
Old 09-11-2008
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  
Old 09-11-2008
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  
Old 09-11-2008
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  
Old 09-12-2008
Quote:
Originally Posted by Annihilannic
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get 20 lines above string found, and 35 below string

i want to search a log for a string. when that string is found, i want to grab the a set number of lines that came before the string, and a set number of lines that come after the string. so if i search for the word "Error" in the /var/log/messages file, how can I output the 20 lines that came... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

stopping tail function after ctrl + c

say i have a statement like this in a script tail -f /opt/blah/blha/user.log > final.log if ;then cat final.log | grep -i "servicer_user" > service.log cat final.log | grep -i "logic_user" > logic.log fi echo "script completed" but when the script is running if i press ctrl + c the... (4 Replies)
Discussion started by: vivek d r
4 Replies

3. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

4. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

5. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

6. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

7. UNIX for Dummies Questions & Answers

How to Tail for a specific string

Hi guys i am tailing a constantly appending log file. However, I am looking for some specific word to be seen only when there is an update. How should I look for the word "{test-0101}" in tail, so that it only shows when there is a line that contains {test-0101} ? Thanks (1 Reply)
Discussion started by: DallasT
1 Replies

8. Solaris

Can't install Unicode::String due to String.so not found

CPAN.pm: Going to build G/GA/GAAS/Unicode-String-2.09.tar.gz Checking if your kit is complete... Looks good Writing Makefile for Unicode::String cp String.pm blib/lib/Unicode/String.pm cp lib/Unicode/CharName.pm blib/lib/Unicode/CharName.pm /usr/bin/perl /usr/perl5/5.8.4/lib/ExtUtils/xsubpp... (5 Replies)
Discussion started by: PatrickBaer
5 Replies

9. Shell Programming and Scripting

bash script tail when string found do something

Okay, I have two scripts, the first one does some stuff, and comes to a point where it has this: Right here it runs a quick script to start something that writes to a log file. /usr/bin/tail -f ${pathVar}/nohup_${servVar}.out | while read -r line do ] && continue cd ${pathVar}... (0 Replies)
Discussion started by: cbo0485
0 Replies

10. Shell Programming and Scripting

Searching for file and stopping at first item found

Hello, I try to write a shell script that would list all files on a directory and stop when it finds the first item specified on a find or ls command. How can I tell to the find or ls command to stop when it finds the first ".doc" file for example ? Thank you (7 Replies)
Discussion started by: davchris
7 Replies
Login or Register to Ask a Question