Proprio come una domanda SO simile , sto provando a monitorare una directory su una macchina Linux per l’aggiunta di nuovi file e vorrei elaborare immediatamente questi nuovi file quando arrivano. Qualche idea sul modo migliore per implementarlo?
Guarda inotify .
Con inotify puoi guardare una directory per la creazione di file.
Innanzitutto assicurati che inotify-tools
installato.
Quindi usarli in questo modo:
logOfChanges="/tmp/changes.log.csv" # Set your file name here. # Lock and load inotifywait -mrcq $DIR > "$logOfChanges" & IN_PID=$$ # Do your stuff here ... # Kill and analyze kill $IN_PID while read entry; do # Split your CSV, but beware that file names may contain spaces too. # Just look up how to parse CSV with bash. :) path=... event=... ... # Other stuff like time stamps? # Depending on the event… case "$event" in SOME_EVENT) myHandlingCode path ;; ... *) myDefaultHandlingCode path ;; done < "$logOfChanges"
In alternativa, usare --format
posto di -c
su inotifywait
sarebbe un'idea.
Solo man inotifywait
e man inotifywatch
per ulteriori informazioni.
Puoi anche usare incron
e usarlo per chiamare uno script di gestione.
Una soluzione che ho pensato è quella di creare un “listener di file” abbinato a un cron job. Non sono pazzo di questo, ma penso che potrebbe funzionare.
fschange (Linux File System Change Notification) è una soluzione perfetta, ma richiede la patch del kernel