I needed to execute an rsync command on MacOS towards a VM when files change in a directory.

I found fswatch that can be installed via brew:



$ brew install fswatch
==> Downloading https://homebrew.bintray.com/bottles/fswatch-1.9.3.sierra.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/45/45de7e8a404f7b7934dc76cd025fae7ac4555a3b8e96e853428fb028d5d80c9b?__gda__=exp=15
######################################################################## 100.0%
==> Pouring fswatch-1.9.3.sierra.bottle.tar.gz
==> Using the sandbox
🍺  /usr/local/Cellar/fswatch/1.9.3: 54 files, 986KB

$ fswatch  --help
fswatch 1.9.3

Usage:
fswatch [OPTION] ... path ...

Options:
 -0, --print0          Use the ASCII NUL character (0) as line separator.
 -1, --one-event       Exit fswatch after the first set of events is received.
     --allow-overflow  Allow a monitor to overflow and report it as a change event.
     --batch-marker    Print a marker at the end of every batch.
     --event=TYPE      Filter the event by the specified type.
 -a, --access          Watch file accesses.
 -d, --directories     Watch directories only.
 -e, --exclude=REGEX   Exclude paths matching REGEX.
 -E, --extended        Use extended regular expressions.
     --format=FORMAT   Use the specified record format.
 -f, --format-time     Print the event time using the specified format.
     --fire-idle-event Fire idle events.
 -h, --help            Show this message.
 -i, --include=REGEX   Include paths matching REGEX.
 -I, --insensitive     Use case insensitive regular expressions.
 -l, --latency=DOUBLE  Set the latency.
 -L, --follow-links    Follow symbolic links.
 -M, --list-monitors   List the available monitors.
 -m, --monitor=NAME    Use the specified monitor.
     --monitor-property name=value
                       Define the specified property.
 -n, --numeric         Print a numeric event mask.
 -o, --one-per-batch   Print a single message with the number of change events.
 -r, --recursive       Recurse subdirectories.
 -t, --timestamp       Print the event timestamp.
 -u, --utc-time        Print the event time as UTC time.
 -v, --verbose         Print verbose output.
     --version         Print the version of fswatch and exit.
 -x, --event-flags     Print the event flags.
     --event-flag-separator=STRING
                       Print event flags using the specified separator.

Available monitors in this platform:

  fsevents_monitor
  kqueue_monitor
  poll_monitor

See the man page for more information.

Report bugs to <enrico.m.crisostomo@gmail.com>.
fswatch home page: <https://github.com/emcrisostomo/fswatch>.

This is the command I devised for my need:

fswatch ../puppet | while read f ; do echo $f ; rsync  -av -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 2222' ../puppet/* root@localhost:/etc/puppetlabs/code/environments/production;  done

When a file changes in the directory ../puppet, fswatch passes in stdout the name of the file(s) that has changed, and keeps running.