Skip to content

Instantly share code, notes, and snippets.

@nico
Last active December 4, 2020 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nico/5bd81b9f887abd48e29f70cdb0978b0c to your computer and use it in GitHub Desktop.
Save nico/5bd81b9f887abd48e29f70cdb0978b0c to your computer and use it in GitHub Desktop.
inotifywait -m with event coalescing
#!/bin/bash
inotifywait -m -e create -r $1 | while read dir action file; do
# Coalese additional queued up events, if any
# If events keep arriving faster than we can read them off stdin here,
# we'll never run the script! If you're worried about that, set
# SECONDS to 0 before this loop and make the conditional also check that
# $SECONDS is smaller than some timeout to make sure that the script runs
# every now and then. The stdin pipe buffer size means there won't be an
# unbounded buildup on stdin -- instead inotifywait will block writing
# output on its side. Not sure that's much better :)
while read -t 0; do
read line
echo skipping $line
done
echo got $dir $action $file
# simulate a command that takes some time:
sleep 3
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment