0

I am running inotifywait on CentOS 6 with the following script:

#!/bin/bash
#
# Try to run inotifywait to execute .buildeach time a file is changed
watch=~/vuejs/files
outfile=/dev/null
build_script=~/.build

inotifywait --monitor --daemon --outfile $outfile --event modify,create,delete --recursive $watch && $build_script

This references a build_script variable which contains:

#!/bin/bash

function build {
        echo $'#\n# Started Build: ' $(date +"%Y-%m-%d %T") >> $output
        cp -r $src_dir $dest_dir
        npm run --prefix $dest_dir build
        if [[ -d $dest_dir/dist ]]; then
                cp -r $dest_dir/dist/* $out_dir
        else
                echo '# Build Error:  ' $(date +"%Y-%m-%d %T") >> $output
        fi
    echo '# Build Completed:  ' $(date +"%Y-%m-%d %T") >> $output
}

output=/home/vue/www/.build.txt
src_dir=/home/vue/vuejs/files/*
dest_dir=/home/vue/vuejs/www
out_dir=/home/vue/www/

build $1 $2 $3

When I run ./.listen from the command line everything seems to run find. I even can do

ps aux | grep inotifywait

and I see that the process is still running, but it only executes the .build script once. Is this because I need to run .listen as a service? How do I make inotifywait execute the .build shell script every time?

Bryan C.
  • 129
  • 1
  • 9

1 Answers1

0

The --daemon option makes inotifywait put itself into the background immediately. the main process exits immediately, and then $build_script is executed.

You should monitor the output file and run the build script every time a line is written:

inotifywait --monitor --daemon --outfile "$outfile" --event modify,create,delete --recursive "$watch"

while read -r line; do
    "$build_script"
done < "$outfile"
Barmar
  • 741,623
  • 53
  • 500
  • 612