[英]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.