I have a log file that text is appended to during the program run. I want to know when a specific string was appended to the end of the file.
All the solutions given here: Monitoring a file until a string is found and here: https://stackoverflow.com/questions/25959870/how-to-wait-till-a-particular-line-appears-in-a-file work, but only on Ubuntu.
I'm trying to do the same thing on Centos 7 and it doesn't seem to end the command.
some things I tried that work in Ubuntu but not in Centos 7:
( tail -f -n0 logfile & ) | grep -q "database start"
tail -n0 -f logfile | sed '/database start/ q'
No idea why it would not work on CentOS 7 but on Ubuntu it does work. I prefer to use a single command instead of sleeping x and grep again, because the line should be outputted multiple times in the file and I want only the one since I started checking the file
I did check the log file and the line did appear, but none of the commands stopped.
tail -f … | grep -q …
quitting when it finds a match? Still, even iftail
is not patched, I expect the command that runs it in the background (i.e. the one with( tail … & )
) to kinda work; so there must be something else.logfile
that is under your total control. Useecho foo >> logfile
andecho "database start" >> logfile
whiletail
runs. Do the commands in question fail to do their job in these circumstances?>
? Usually for logfiles>>
is better. If more than one process writes then>>
is definitely better (see this question). (2) Anyway, for me it works also with>
buttail
exits when the file is truncated, it may be not what you want. Don't use>
then. The only case where it "doesn't work" is if the file containsdatabase start
as a sole line already acknowledged bytail
(but not printed due to-n0
); executingecho "database start" > logfile
brings the file to the same state;tail
cannot notice (cont'd)grep
without-q
and see if it prints). Maybe the problem is not in "doesn't end", maybe the problem is in "doesn't see". How exactly did you "check the log file and the line did appear"? Was the file removed and created anew? Maybe you needtail -F
.