简体   繁体   English

使用inotifywait并行处理两个文件

[英]Using inotifywait to process two files in parallel

I am using: 我在用:

inotifywait -m -q -e close_write --format %f . | while IFS= read -r file; do
cp -p "$file" /path/to/other/directory
done

to monitor a folder for file completion, then moving it to another folder. 监视文件夹的文件完成情况,然后将其移动到另一个文件夹。

Files are made in pairs but at separate times, ie File1_001.txt is made at 3pm, File1_002.txt is made at 9pm. 文件是成对创建的,但要在不同的时间进行,即File1_001.txt在下午3点创建,File1_002.txt在晚上9点创建。 I want to monitor for the completion of BOTH files, then launch a script. 我想监视两个文件的完成情况,然后启动一个脚本。

script.sh File1_001.txt File1_002.txt

So I need to have another inotifywait command or a different utility, that can also identify that both files are present and completed, then start the script. 因此,我需要另一个inotifywait命令或其他实用程序,它们也可以识别两个文件均已存在且已完成,然后启动脚本。

Does anyone know how to solve this problem? 有谁知道如何解决这个问题?

I found a Linux box with inotifywait installed on it, so now I understand what it does and how it works. 我发现一个装有inotifywait的Linux inotifywait ,所以现在我了解了它的作用和工作方式。 :) :)

Is this what you need? 这是您需要的吗?

#!/bin/bash

if [ "$1" = "-v" ]; then
        Verbose=true
        shift
else
        Verbose=false
fi

file1="$1"
file2="$2"

$Verbose && printf 'Waiting for %s and %s.\n' "$file1" "$file2"

got1=false
got2=false
while read thisfile; do
        $Verbose && printf ">> $thisfile"
        case "$thisfile" in
                $file1) got1=true; $Verbose && printf "... it's a match!" ;;
                $file2) got2=true; $Verbose && printf "... it's a match!" ;;
        esac
        $Verbose && printf '\n'
        if $got1 && $got2; then
                $Verbose && printf 'Saw both files.\n'
                break
        fi
done < <(inotifywait -m -q -e close_write --format %f .)

This runs a single inotifywait but parses its output in a loop that exits when both files on the command line ( $1 and $2 ) are seen to have been updated. 这将运行一个inotifywait但会在循环中解析其输出,当看到命令行上的两个文件( $1$2 )都已更新时,该循环将退出。

Note that if one file is closed and then later is reopened while the second file is closed, this script obviously will not detect the open file. 请注意,如果关闭了一个文件,然后又关闭了第二个文件,则稍后再打开,则该脚本显然不会检测到打开的文件。 But that may not be a concern in your use case. 但这可能不是您的用例所关心的。

Note that there are many ways of building a solution -- I've shown you only one. 请注意,构建解决方案的方法有很多-我仅向您展示了一种。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过FTP将文件添加到git存储库后,在脚本中使用inotifywait来自动执行git commit - using inotifywait in a script to automate a git commit when files have been added to git repository via FTP 使用Expect和inotifywait监视Linux上的文件夹中的更改 - Using expect and inotifywait to watch for changes in a folder on linux 如何使用inotifywait监视带有时间和文件的目录? - How to monitor a directory with time and file using inotifywait? 在脚本中使用inotifywait -m而不创建额外的进程? - Use inotifywait -m in script without creating extra process? 为什么此inotifywait shellscript用两个PID进行购买? - Why does this inotifywait shellscript shop up with two PIDs? inotifywait -m 在长时间运行过程后不处理超过 1 个文件 - inotifywait -m does not process more than 1 file after long running process 如何在不使用inotifywait的情况下监视目录中的文件更改? - How to monitor a directory for file changes without using inotifywait? 以下代码是否在C ++中运行从单个父进程并行执行的两个子进程? - Does the following code runs two child process executing in parallel from a single parent process in C++? 使用子流程模块并行工作(多流程) - Using subprocess module to work in parallel (Multi-process) 嵌套 GNU Parallel 处理多个大文件并将每个文件数据拆分为队列处理 - nesting GNU Parallel to process multiple huge files and split each file data to be processed as queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM