檢測資料夾更改

檢測資料夾更改

在 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

相關內容