如何每秒从缓冲区读取?

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

我编写的脚本将在更改代码后重新加载我的应用。

到目前为止,我拥有将提供来自更改的服务名称的部分:

inotifywait $ENVPATH --recursive --monitor --event CREATE --event MODIFY  --event DELETE | grep --line-buffered -Eiv ".idea|.phpstorm.meta.php|runtime|.swp|.log"  

但是当我编写代码时,我不想每秒有多个触发重载事件,因此我需要缓冲此流。我想读取所有可用的数据,直到每隔x秒。

如何使用bash进行操作,到目前为止,我只知道这种读取数据的方式,但它不适合我的需求

while read line
do
  echo "$line"
done 
linux bash shell
2个回答
1
投票

您可以在指定的时间内主动忽略inotifywait输出的所有内容。

inotifywait ... |
while read line
do
     echo "$line"
     # ignore input for 1 second
     timeout 1 cat >/dev/null
done 

0
投票

read -t 0将检查是否有可用输入而不实际读取。您可以使用它来检查是否存在任何应忽略的缓冲事件。

inotifywait -rm -e CREATE -e MODIFY -e DELETE "$ENVPATH" \
    --exclude '\.idea$|\.phpstorm.meta.php$|runtime|\.swp$|\.log$' |
    # Block until there's an event.
    while read -r dir event path; do
        # Discard all remaining events.
        while read -t 0; do read -r dir event path; done
    done

请注意,您可以使用--exclude直接从inotifywait中过滤掉文件。

© www.soinside.com 2019 - 2024. All rights reserved.