0

I am using inotifywait to copy files and the build them. But how do I get it to copy all the changed files and build once? A simple script will build 3 times per file changed (something to do with temporary files when I save?)

inotifywait --event modify -m -r -q --format '%w%f' "/home/$1/src/$2"  | while read filename event
do
cp -f "$filename" "$TARGET_DIR"
make all

I can can get it to build once, and that works if I am changing one file at a time, but if you change more than one file at a time, then only the first gets copied before it builds.

LASTCALLED=$(date +%s)
inotifywait --event modify -m -r -q --format '%w%f' "/home/$1/src/$2"  | while read filename event
do
   cp -f "$filename" "$TARGET_DIR"
   if [[ $(($(date +%s)-$LASTCALLED)) -gt 5 ]]; then
      make all
   LASTCALLED=$(date +%s)

Is there an easy way to check that inotifywait is done looping before I build?

2 Answers 2

1

You could let inotifywait let you know about one change and then let rsync pick up the rest.

inotifywait --event modify -m -r -q "$SOURCE_PATH"  | while read filename event
do
   rsync [flags] "$SOURCE_PATH" "$TARGET_DIR"
   make all
1
  • This is better than what I had, but I still need to modify the files twice. inotifywait will notice that the file is about to be updated, so it will rsync the files and build before any of the files get copied.
    – Holtorf
    Mar 5, 2020 at 22:26
0

This is not ideal, but something that mostly works is to build in a separate thread on a delay, so that all the files have time to get copied before you build.

LASTCALLED=$(date +%s)
inotifywait --event modify -m -r -q --format '%w%f' "/home/$1/src/$2"  | while read filename event
do
   cp -f "$filename" "$TARGET_DIR"
   if [[ $(($(date +%s)-$LASTCALLED)) -gt 5 ]]; then
      ( sleep 1; make all ) &
   LASTCALLED=$(date +%s)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .