Skip to content
Discussion options

You must be logged in to vote

To prevent missed events and race conditions during high-throughput file modifications, use inotifywait with monitor mode (-m) filtered on write completion rather than initial file touches:

#!/usr/bin/env bash
WATCH_DIR="/target/directory"

inotifywait -m -e close_write,moved_to --format '%w%f' "$WATCH_DIR" | while read -r filepath; do
    # $filepath is guaranteed to have finished writing
    echo "Processing: $filepath"
done

Filtering on close_write guarantees the writing process has closed its file descriptor before downstream handlers access it.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by amitpadhan525
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants