¿Existe alguna forma de detectar si un archivo se coloca en una carpeta (cambio de contenido del directorio) (no en un subdirectorio) y ejecutar un script php (sin Cron/Crontab) a través de CLI en Ubuntu 18.04?
Respuesta1
Como se indicó en la respuesta anterior, instale inotify-tools
:
sudo apt install -y inotify-tools
Ahora puedes usar el comando inotifywait:
inotifywait -m /your/dir -e create -e move |
while read path action file; do
# your preferred command here
done
Con inotifywait --help
usted obtiene los eventos que puede monitorear:
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
Respuesta2
Uso inotify
que debería ser parte del inotify-tools
paquete.
Un script que se ejecuta en segundo plano podría verse así
#!/bin/sh
while :
do
inotifywatch -e moved_to -e create /watched/dir && {
php -f /path/to/script.php
}
done