如何等待消息出现在登录 shell 中

问题描述 投票:0回答:2

您能否提供简洁的解决方案来阻止脚本执行,直到文本片段出现在给定文件中?

bash shell
2个回答
12
投票

永远等待

grep -q 'ProducerService started' <(tail -f logs/batch.log)

超时等待

timeout 30s grep -q 'ProducerService started' <(tail -f logs/batch.log)

等待超时,通知错误

timeout 30s grep -q 'ProducerService started' <(tail -f logs/batch.log) || exit 1  

等待下一个插入的实例(由@Dan-Dev)

timeout 30s grep -q 'ProducerService started' <(tail -n0 -f logs/batch.log)

1
投票

使用

inotifywait

inotifywait 有效地等待文件的更改

示例:-

  1. 杀死要阻塞的进程
  2. inotifywait -q -e modify /path/to/file/containing/snippet
  3. 检查文件中的更改
  4. 如果更改匹配,则重新启动脚本
© www.soinside.com 2019 - 2024. All rights reserved.