检测文件夹变化

检测文件夹变化

是否存在任何方法可以检测文件是否放在文件夹中(目录内容更改)(而不是子目录)并通过 ubuntu 18.04 中的 CLI 执行 php 脚本(没有 Cron/Crontab)?

答案1

如上一个答案所述,安装inotify-tools

sudo apt install -y inotify-tools

现在您可以使用命令inotifywait:

inotifywait -m /your/dir -e create -e move |
while read path action file; do
  # your preferred command here
done

通过inotifywait --help获取您可以监控的事件:

Events:
    access      file or directory contents were read
    modify      file or directory contents were written
    attrib      file or directory attributes changed
    close_write file or directory closed, after being opened in
                writable mode
    close_nowrite   file or directory closed, after being opened in
                read-only mode
    close       file or directory closed, regardless of read/write mode
    open        file or directory opened
    moved_to    file or directory moved to watched directory
    moved_from  file or directory moved from watched directory
    move        file or directory moved to or from watched directory
    create      file or directory created within watched directory
    delete      file or directory deleted within watched directory
    delete_self file or directory was deleted
    unmount     file system containing file or directory unmounted

答案2

使用inotify应该是inotify-tools包的一部分的内容。

在后台运行的脚本可能如下所示

#!/bin/sh

while : 
do
  inotifywatch -e moved_to -e create /watched/dir && {
    php -f /path/to/script.php
  }
done

相关内容