我在使用Linux bash脚本时遇到问题,无法跟踪目录上的文件更改。
我偶然发现了inotify(在这种情况下是inotifywait),这是一个非常好的工具,可以运行我的代码。
但它运行得太频繁,有人可以告诉我如何阻止这些代码在相同的文件上反复运行吗?
为了澄清一下,脚本运行的目录是我的照片重命名文件夹,此处删除的所有照片/电影都会根据exif信息重命名,然后移动到照片文件夹。
删除所有内部代码以继续讨论inotifywait命令。
#!/bin/bash
PATH=/data/RenamePhoto
NEWPATH=/mnt/photo
cd $PATH
# inotify will watch over the folder
/usr/bin/inotifywait --monitor \
--recursive \
--event close_write \
--format %f ./ | while read FILE; do
/bin/sleep 5
.....more code here......
done
以下是inotifywait的一些输出:
Setting up watches. Beware: since -r was given, this may take a while! Watches established. File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory File not found: IMG_3482.JPG /bin/mv: cannot stat ‘IMG_3482.JPG’: No such file or directory
正如您所看到的那样,它继续在同一个文件上运行,导致此行为是因为在通过网络写入时正在更改文件。
我添加了睡眠5'给文件写命令一些时间来完成,但inotifywiat有一个缓冲区,你可以看到。
每个文件只运行一次这个rename和mv命令吗?