Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何等待消息出现在登录shell中_Bash_Shell - Fatal编程技术网

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

Bash 如何等待消息出现在登录shell中,bash,shell,Bash,Shell,能否提供简洁的解决方案,在给定文件中出现文本片段之前阻止脚本的执行?使用inotifywait inotifywait有效地等待对文件的更改 例如:- 终止要阻止的进程 inotifywait-q-e modify/path/to/file/containing/snippet 检查文件中的更改 如果更改匹配,则重新启动脚本 永远等待 grep -q 'ProducerService started' <(tail -f logs/batch.log) grep-q“ProducerSe

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

使用
inotifywait

inotifywait有效地等待对文件的更改

例如:-

  • 终止要阻止的进程
  • inotifywait-q-e modify/path/to/file/containing/snippet
  • 检查文件中的更改
  • 如果更改匹配,则重新启动脚本
  • 永远等待

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

    grep-q“ProducerService start”在很大程度上取决于缓冲和您使用的操作系统。Linux提供了几种基于
    inotify
    man7inotify
    )的非标准接口,但是
    bash
    中没有这样做的功能。即使是
    inotify
    也只会在刷新缓冲区后检测到更改。如果编写程序使用大的缓冲区,那么在程序关闭之前(通常刷新缓冲区),文件大小可能不会有可检测的变化。请注意,使用
    -KILL
    信号(
    KILL-9
    )终止进程可能会导致缓冲区未刷新和数据丢失。
    timeout 30s grep -q 'ProducerService started' <(tail -f logs/batch.log)
    
    timeout 30s grep -q 'ProducerService started' <(tail -f logs/batch.log) || exit 1