Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

bash - Exit tail upon string detection

I'm writing a barrier to stall the execution of a script until a certain keyword is logged. The script is pretty simple:

tail -F -n0 logfile.log | while read LINE; do
    [[ "$LINE" == *'STOP'* ]] && echo ${LINE} && break;
done

or

tail -F -n0 logfile.log | grep -m1 STOP

The thing is it doesn't quit as soon as the keyword is detected, but only after the next line is written. I.e:

printf "foo
"  >> logfile.log  # keeps reading
printf "foo
"  >> logfile.log  # keeps reading
printf "STOP
" >> logfile.log  # STOP printed
printf "foo
"  >> logfile.log  # code exits at last

Unfortunately I can't rely on the fact that another line will be logged after the "STOP" (not within an interval useful for my purposes at least).

The workaround found so far is to tail also another file I know for sure gets updated quite frequently, but what is the "clean" solution so that the code will exit right after it logs STOP?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In , when executing a command of the form

command1 | command2

and command2 dies or terminates, the pipe which receives /dev/stdout from command1 becomes broken. This, however, does not terminate command1 instantly.

So to achieve what you want is to use process substitution and not a pipe

awk '/STOP/{exit}1' < <(tail -f logfile)

When you use , you can see the behaviour in a bit more detail:

$ touch logfile
$ tail -f logfile | awk '/STOP/{exit}1;END{print "end"}'

This awk program will check if "STOP" is seen, and if not print the line again. If "STOP" is seen it will print "end"

When you do in another terminal

$ echo "a" >> logfile
$ echo "STOP >> logfile
$ echo "b" >> logfile

You see that prints the following output:

a             # result of print
end           # awk test STOP, exits and executes END statement

Furthermore, if you look more closely, you see that is at this point already terminated.

ps before sending "STOP":

13625 pts/39   SN     0:00  |        \_ bash
32151 pts/39   SN+    0:00  |            \_ tail -f foo
32152 pts/39   SN+    0:00  |            \_ awk 1;/STOP/{exit}1;END{print "end"}

ps after sending "STOP":

13625 pts/39   SN     0:00  |        \_ bash
32151 pts/39   SN+    0:00  |            \_ tail -f foo

So the awk program terminated, but tail did not crash because it is not yet aware the pipe is broken as it did not attempt to write to it.

When you do the following in the terminal with the pipeline, you see the exit status of tail:

$ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
$ 141 0

Which states that awk terminated nicely, but tail terminated with exit code 141 which means SIGPIPE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...