'grep的-q“与”尾-f“退出 [英] 'grep -q' not exiting with 'tail -f'
问题描述
我想实现一个脚本,等待在日志文件中的特定信息。一旦消息被记录的话,我想继续脚本。
I am trying to implement a script that wait for a specific message in a log file. Once the message is logged then I want to continue the script.
下面就是我与尾尝试-f
和的grep -q
:
# tail -f logfile | grep -q 'Message to continue'
的的grep
从没有放弃过,所以它永远等待,即使消息继续的文件中被记录下来。
The grep
never quit and so it waits forever even if 'Message to continue' is logged in the file.
当我运行这个没有 -f
这似乎很好地工作。
When I run this without -f
it seems to work fine.
推荐答案
尾-f
将读取的文件,并显示行以后添加,也不会终止(除非像 SIGTERM
信号发送)。 的grep
是不是这里的拦网环节,尾-f
是。 的grep
将从管道读取,直到它被关闭,但它永远不会是因为尾-f
不退出,并保持管开。
tail -f
will read a file and display lines later added, it will not terminate (unless a signal like SIGTERM
is sent). grep
is not the blocking part here, tail -f
is. grep
will read from the pipe until it is closed, but it never is because tail -f
does not quit and keep the pipe open.
一个解决您的问题可能会被(未测试,并很可能表现不佳):
A solution to your problem would probably be (not tested and very likely to perform badly):
tail -f logfile | while read line; do
echo $line | grep -q 'find me to quit' && break;
done
这篇关于'grep的-q“与”尾-f“退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!