폴더 변경 감지

폴더 변경 감지

파일이 폴더(디렉터리 내용 변경)(하위 디렉터리 아님)에 있는지 감지하고 우분투 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

관련 정보