Detectando alterações de pasta

Detectando alterações de pasta

existe alguma maneira de detectar se um arquivo é colocado em uma pasta (mudança de conteúdo do diretório) (não subdiretório) e executar um script php (sem Cron/Crontab) via CLI no Ubuntu 18.04?

Responder1

Conforme declarado na resposta anterior, instale inotify-tools:

sudo apt install -y inotify-tools

Agora você pode usar o comando inotifywait:

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

Com inotifywait --helpvocê obtém os eventos que pode monitorar:

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

Responder2

Use inotifyo que deve fazer parte do inotify-toolspacote.

Um script executado em segundo plano poderia ter esta aparência

#!/bin/sh

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

informação relacionada