Logfile monitoring with logfile replacement

Bonjour,
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
done

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.

Regards

---------- 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.

@Warluck

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
done

$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:

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.

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.

The following seems to do what you want:

#!/bin/ksh
logfile=${1:-logfile}
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
	fi
done&

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).

1 Like