Skip to content

Instantly share code, notes, and snippets.

@Phlogi
Last active August 3, 2022 12:08
Show Gist options
  • Save Phlogi/e20a3f3c2be5ac631ab51817e12103df to your computer and use it in GitHub Desktop.
Save Phlogi/e20a3f3c2be5ac631ab51817e12103df to your computer and use it in GitHub Desktop.
Polling adé
#!/usr/bin/env bash
MONITORDIR="/tmp/test_new_files_dir/"
NOW_FILE="/tmp/catch_file_changes_now.touch"
DELAY_FILE="/tmp/catch_file_changes.touch"
AWAIT_TIME=20 # seconds
AWAIT_TIME_STR="+${AWAIT_TIME}seconds"
CMD="echo \"da command\""
echo "[start] $(date)"
await_and_run () {
echo " [${1}] waiting ${AWAIT_TIME} + 1 seconds inside await function... (eventually running your command)"
sleep ${AWAIT_TIME}
sleep 1 # ensure it's really newer
touch ${NOW_FILE}
if [[ ${NOW_FILE} -nt ${DELAY_FILE} ]]
then
echo " [${1}] awaiting succeeded (no new files added during ${AWAIT_TIME} seconds), will run command > ${CMD} <"
eval "${CMD}"
else
echo " [${1}] awaing aborted, as newer file was added, will not run command yet..."
fi
}
# -m: Instead of exiting after receiving a single event, execute indefinitely.
# -r: Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not traversed. Newly created subdirectories will also be watched.
# -e: Event type, we use create: A file or directory was created within a watched directory.
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
echo "File ${NEWFILE} has been created beneath watched folder ${MONITORDIR}"
touch -d"+20seconds" ${DELAY_FILE}
# run function in background to not block detection of newer files coming in
await_and_run ${NEWFILE} &
echo "do loop end of inotifywait, waiting for next newfile..."
done
# cleanup
rm -vf "${NOW_FILE}"
rm -vf "${DELAY_FILE}"
echo "[done] $(date)"
@Phlogi
Copy link
Author

Phlogi commented Aug 3, 2022

Sample run:

[start] Wed 03 Aug 2022 02:06:36 PM CEST
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
File /tmp/test_new_files_dir/yesef1 has been created beneath watched folder /tmp/test_new_files_dir/
do loop end of inotifywait, waiting for next newfile...
  [/tmp/test_new_files_dir/yesef1] waiting 20 + 1 seconds inside await function... (eventually running your command)
File /tmp/test_new_files_dir/yesef2 has been created beneath watched folder /tmp/test_new_files_dir/
do loop end of inotifywait, waiting for next newfile...
  [/tmp/test_new_files_dir/yesef2] waiting 20 + 1 seconds inside await function... (eventually running your command)
File /tmp/test_new_files_dir/yesef3 has been created beneath watched folder /tmp/test_new_files_dir/
do loop end of inotifywait, waiting for next newfile...
  [/tmp/test_new_files_dir/yesef3] waiting 20 + 1 seconds inside await function... (eventually running your command)
  [/tmp/test_new_files_dir/yesef1] awaing aborted, as newer file was added, will not run command yet...
  [/tmp/test_new_files_dir/yesef2] awaing aborted, as newer file was added, will not run command yet...
  [/tmp/test_new_files_dir/yesef3] awaiting succeeded (no new files added during 20 seconds), will run command > echo "da command" <
da command```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment