I want to do a tail -F on a file until matching a pattern. I found a way using awk, but IMHO my command is not really clean. The problem is that I need to do it in only one line, because of some limitations.
tail -n +0 -F /tmp/foo | \
awk -W interactive '{if ($1 == "EOF") exit; print} END {system("echo EOF >> /tmp/foo")}'
The tail will block until EOF appears in the file. It works pretty well. The END block is mandatory because awk's "exit" does not exit right away. It makes awk to eval the END block before quitting. The END block hangs on a read call (because of tail), so the last thing I need to do, is to write another line in the file to force tail to exit.
Does someone know a better way to do that?
Here's an extended version of Jon's solution which uses sed instead of grep so that the output of tail goes to stdout:
This works because sed gets created before tail so $! holds the PID of tail
The main advantage of this over the sh -c solutions is that killing a sh seems to print something to the output such as 'Terminated' which is unwelcome
This is something Tcl is quite good at. If the following is "tail_until.tcl",
Then you'd do
tail_until.tcl /tmp/foo EOF
ready to use for tomcat =
sh -c 'tail -f --pid=$$ catalina.out | { grep -i -m 1 "Server startup in" && kill $$ ;}'
for above scenario :
I've not results with the solution:
There is some issue related with the buffer because if there aren't more lines appended to the file, then sed will not read the input. So, with a little more research i came up with this:
The script is in https://gist.github.com/2377029
Here the main problem is with
$$
. If you run command as is,$$
is set not to sh but to the PID of the current shell where command is run.To make kill work you need to change
kill $$
tokill \$$
After that you can safely get rid of
--pid=$$
passed to tail command.Summarising, following will work just fine:
Optionally you can pass
-n
tosed
to keep it quiet :)