Visit Our UNIX and Linux User Community

Logfile monitoring with logfile replacement

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logfile monitoring with logfile replacement
# 1  
Old 11-14-2014
Logfile monitoring with logfile replacement

I've wrote a script to monitor a logfile in realtime. It is working almost perfeclty except for two things.

The script use the following technique :
tail -fn0 $logfile | \
while read line ; do
... some stuff

First one, I'd like a way to end the monitoring script if a certain string in sent to the logfile. I have used BREAK and EXIT which work but only at the next pass of the loop. Just to be sure I am clear :
Pass 1 of the loop, find the string that ends the script, send break to the loop.
No exit until Pass 2 of the loop that occurs when a new line is inserted to the logfile

I would like to end the script right away and not wait a new line to be added to the monitored logfile.

Second thing. I would like a way to reset the script, explain :

Sometimes, I need to delete(rotate) the logfile and create a new one. When I do that, my monitor script continues to run but seems to "lose" the logfile, I need to kill the script and launch it anew. Is there a way to tell the script restart the loop with the new log without having to end the script?

thanks for your time and expertise.


---------- Post updated at 06:33 PM ---------- Previous update was at 06:20 PM ----------

I am having a quick answer to my second question. I use tail -F instead of tail -f, the "-F" tells tail to follow the file and retry which is exactly what I want.
# 2  
Old 11-15-2014

If you don't see any one posting comments on you thread is a pretty good indication that the inquire is not clear. You might need to give more information or formulate you query differently.

As it happens, I am not quite sure what you mean.
tail -fn0 $logfile | \
while read line ; do
... some stuff

$logfile will keep logging until you stop the mechanism that records the log entries, and this is independent of displaying its content with tail and piping it to a while read loop. It is also independent of that loop.

Thus, this statement of yours is not clear:
I would like to end the script right away and not wait a new line to be added to the monitored logfile.
Even if the script stops the mechanism that logs will keep going.

Now, if you want to stop the script right when it encounters a given string, you must read it first, identify it as a show stopper, and exist the script. Therefore, it must be done, before any of the other do ...some stuff.

But, please, expand further, since I'm quite sure this will not satisfy your query.

Last edited by Aia; 11-15-2014 at 02:12 AM.. Reason: Grammar correction
# 3  
Old 11-15-2014
What @Warluck means is that the tail -F at the LHS of the pipe will not stop immediately once the while read loop at the RHS of the pipe finishes. Instead it will exit at the moment that it tries to write the next line to the pipe and can no longer do that.
# 4  
Old 11-15-2014
The following seems to do what you want:
tail -Fn0 "$logfile" |
while read -r line
do	ls -li "$logfile"
	printf 'Read "%s" from "%s"\n' "$line" "$logfile"
	if [ "$line" = "EOF" ]
	then	echo exiting
		kill 0
	else	echo continuing

This script terminates when a line containing just EOF is added to your log file.

I use the Korn shell most of the time; this was tested with both ksh and bash. It should work with any POSIX conforming shell.

The kill 0 sends a SIGTERM signal to every process in the current process group (i.e., the shell running the while loop and the tail, so the tail will terminate without waiting for another line to be written to your log file after the EOF line is read).
This User Gave Thanks to Don Cragun For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #288
Difficulty: Easy
In Linux, Logical Volume Manager (LVM) is a device mapper target that provides sound card I/O management for the Linux kernel.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Logfile Reading

I am reading through a log file that has multiple entries: xx-xxxx-xxx-130111090001 <XML> ... ... </XML> ... ... ... xx-xxxx-xxx-130111100001 <XML> ... ... </XML> There are 2 parts: 1) Take the XML statement and parse out. I have that with a sed script sed -n '/<XML>/,/XML>/p'... (6 Replies)
Discussion started by: SivaAfi
6 Replies

2. Shell Programming and Scripting

Parsing Logfile

Hi, I need to continuously monitor a logfile to get the log information between a process start and end. the logfile look like this abcdddddddddd cjjckkkkkkkkkkkk abc : Process started aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbb abc... (6 Replies)
Discussion started by: Byorg
6 Replies

3. UNIX for Dummies Questions & Answers

writing to a logfile

I cannot get anything to go to my log. This is what i see on my screen when i run my korn shell. cd ok, cwd=/export/home/tsp_inst/TSP pget: /nas4/edata/tsp/rawdata/test2.gz: File exists But i cant get it to write it to my log. cd /this/is/my/newdirectory lftp -e 'pget -c -n 4... (1 Reply)
Discussion started by: tyngsboro
1 Replies

4. Shell Programming and Scripting

looking for string in logfile

I have a shell script that used to look for a particular sting in the last line of a log file. Howeve this string now has moved to the 3rd or 4th line from bottom. Can anyone point me to how i easiest chage this one to look within the last frew lines (5 or so) for the particular string rather... (6 Replies)
Discussion started by: jjlinux
6 Replies

5. UNIX for Dummies Questions & Answers

change value of logfile

Hi, I have a log file like exyymmdd.log and i need to do is read the log file and inside the logfile if i find the word which starts with then i need to replace the value of is unique) with its corresponding title. A list of id( and its title are... (5 Replies)
Discussion started by: umapearl
5 Replies

6. Shell Programming and Scripting

logfile parsing

I thought I was pretty handy with awk until I got this one. :) I'm trying to parse a log file where the events could have different delimiters (2 scripts is ok), the errors are spread over multiple lines, and I"m trying to figure out how to not read the same lines that have already been read. ... (1 Reply)
Discussion started by: linkslice
1 Replies

7. UNIX for Dummies Questions & Answers

Logfile manipulation

Hi First of all I m a complete newbie to Linux ... just started working on it exactly a week ago. I know a bit of programming in C but not very good in it.:D I was given task in my workplace and need some help nAJLR02F030879 9805 Thu Nov 19 13:27 <> ... (2 Replies)
Discussion started by: abilash.amara
2 Replies

8. Shell Programming and Scripting


hi iam new of the ksh script.iwant in formation of how to call in logfile in ksh scripts. if the meaning in ksh. please help me thanks naveen.g (1 Reply)
Discussion started by: naveeng.81
1 Replies

9. Shell Programming and Scripting

last month's logfile

hi friends I need a shell script which will do the following Task Enter the month : if you enter 1 then it ll show you last 1 month's (starting from today).log file in the current directry. if you enter 4 then it ll show you last 4 month's (starting from today).log file in the current... (2 Replies)
Discussion started by: deep_kol
2 Replies

10. Shell Programming and Scripting

sort a logfile

Hi experts, I need help to sort a big logfile. logfile: ------- 67712 dkjd jd jj jjjj ------ kjkj jhjh kkk yggg lll hhh gffgf jj -------- i kkk kllkkl ------- Now I want every think between the "------" in one line. Normaly with paste no problem but you can see that the... (7 Replies)
Discussion started by: joerg
7 Replies

Featured Tech Videos