监视目录以获取没有第三方包的更改

时间:2022-04-18 08:55:47

Many other questions is about this around *. I haven't found any where the result doesn't use any 3rd party package.

围绕*还有很多其他问题。我没有找到任何结果不使用任何第三方包的地方。

The problem is that I'm using Hetzner and they want allow me to install any packages.

问题是我正在使用Hetzner,他们希望允许我安装任何软件包。

I can use PHP, Python, Java (think pretty much the standard collection)

我可以使用PHP,Python,Java(几乎认为是标准集合)

How to monitor a directory (recursive) for new files and get the references into another file.

如何监视新文件的目录(递归)并将引用引入另一个文件。

I have tried something like this:

我尝试过这样的事情:

while true; do
    last=`cat log.txt`
    find "$UPLOAD_FOLDER"'/.' -type f > 'log.txt'
    now=`cat log.txt`

    diff -n <(echo "$last") <(echo "$now") >> 'queue.txt'

    sleep 60;
done;

But it's unreliable and it pollutes queue.txt with d3a,b5a, etc..

但它不可靠,它用d3a,b5a等污染queue.txt。

2 个解决方案

#1


1  

Probably the easiest is to use a marker file:

可能最简单的方法是使用标记文件:

touch markerFile
while true
do
    find "$UPLOAD_FOLDER"'/.' -type f -newer markerFile >> 'queue.txt'
    touch markerFile
    sleep 60
done

Might be a slight race condition between the find and the touch?

在发现和触摸之间可能会出现轻微的竞争条件?

A more complicated double buffering solution as suggested in the comments:

一个更复杂的双缓冲解决方案,如评论中所示:

touch markerFileA
touch markerFileB
while true
do
    touch markerFileB
    find "$UPLOAD_FOLDER"'/.' -type f -newer markerFileA ! -newer markerFileB >> 'queue.txt'
    sleep 60
    touch markerFileA
    find "$UPLOAD_FOLDER"'/.' -type f -newer markerFileB ! -newer markerFileA >> 'queue.txt'
    sleep 60
done

#2


0  

You could use sed to remove the undesired output:

您可以使用sed删除不需要的输出:

while :; do

    find "$UPLOAD_FOLDER/." -type f > "log1.txt"

    if [ -f "log2.txt" ]; then
        diff log1.txt log2.txt | sed '/^[^<]/d;s/^< //'

        fi

    mv "log1.txt" "log2.txt"

    sleep 60

done

#1


1  

Probably the easiest is to use a marker file:

可能最简单的方法是使用标记文件:

touch markerFile
while true
do
    find "$UPLOAD_FOLDER"'/.' -type f -newer markerFile >> 'queue.txt'
    touch markerFile
    sleep 60
done

Might be a slight race condition between the find and the touch?

在发现和触摸之间可能会出现轻微的竞争条件?

A more complicated double buffering solution as suggested in the comments:

一个更复杂的双缓冲解决方案,如评论中所示:

touch markerFileA
touch markerFileB
while true
do
    touch markerFileB
    find "$UPLOAD_FOLDER"'/.' -type f -newer markerFileA ! -newer markerFileB >> 'queue.txt'
    sleep 60
    touch markerFileA
    find "$UPLOAD_FOLDER"'/.' -type f -newer markerFileB ! -newer markerFileA >> 'queue.txt'
    sleep 60
done

#2


0  

You could use sed to remove the undesired output:

您可以使用sed删除不需要的输出:

while :; do

    find "$UPLOAD_FOLDER/." -type f > "log1.txt"

    if [ -f "log2.txt" ]; then
        diff log1.txt log2.txt | sed '/^[^<]/d;s/^< //'

        fi

    mv "log1.txt" "log2.txt"

    sleep 60

done