我想在 bash 脚本中启动一个进程,如下所示:
myProcess -p someparameter > log.txt &
然后我想在log.txt
包含defined sentence to wait for
后立即停止该过程.所以我不是在等待文件,而是在等待日志文件中的条目!
最好的方法是什么(不煎炸我的处理器,因此没有像疯了一样运行的循环)?
在评论中,我对这种解决方案的稳定性表示担忧,因为在监视器检测到日志文件中的消息时,该过程可能已经进一步进行。
你说:The whole thing is about doing an automated analysis for one time on about 50 different inputs. Job done. It is not an academic exercise
.
好的,对于这样的用例,您可以执行以下操作:
# Use stdbuf -oL to make sure the process output
# will get line buffered (instead of default block-buffered)
stdbuf -oL ./process -p param > logfile &
pid=$!
# Again make sure that the output of tail get's
# line buffered. grep -m1 stops after the first
# occurence
stdbuf -oL tail -f logfile | grep -m1 "PATTERN" && kill "${pid}"
注意:我想在这个例子中,process
kill
会很好地退出。