2

My current solution is to use the python library watchdog and the bash snippet (originally taken from here).

watchmedo shell-command client/js/src/templates/ proto/ --recursive \
  --patterns="*.soy;*.proto" \
  --command="echo \"WATCHMEDO file changed - rebuilding\"; make genfiles;"

Basically I'm watching a few template files, and then running make genfiles automatically if one of them changes.

I'm wondering if there's a way to do this in pure bash? I'd rather not have all my devs have to depend on that Python library.

I'm on OSX.

clt60
  • 62,119
  • 17
  • 107
  • 194
Ross117
  • 1,020
  • 1
  • 12
  • 20
  • maybe you could use a combination of `md5sum` and environment variables of the form `WATCHME_`. Store the original checksum in the file's env. var and then run `md5sum` for the file and if that result doesn't match the file's env var you know it changed. – Red Cricket Apr 05 '14 at 18:00
  • check this question: http://stackoverflow.com/questions/1515730/is-there-a-command-like-watch-or-inotifywait-on-the-mac – clt60 Apr 05 '14 at 18:02
  • It looks like the best solution there is to use this library. https://github.com/alandipert/fswatch. Neat little thing, but then that shifts my dependency from python to homebrew. I guess there are just no builtin commands that do what I want. – Ross117 Apr 05 '14 at 18:09
  • how _fast_ you need react on change? How often changes occurs? As red cricket suggest to you, it is possible to make an short find-mdsum combination, but all depends on your needs... – clt60 Apr 05 '14 at 18:16
  • or, maybe you can get usable output from the `fs_usage` command. (need run as root) – clt60 Apr 05 '14 at 18:25
  • Preferably instantly. i.e. you save the file, it recompiles, and then it's available as soon as you refresh the browser page (because this is a web app). – Ross117 Apr 05 '14 at 18:26

2 Answers2

8
#!/bin/bash

watched_files=$@  # pass watched files as cmd line arguments

if [ -z "$watched_files" ]; then
    echo "Nothing to watch, abort"
    exit
else
    echo "watching: $watched_files"
fi

previous_checksum="dummy"
while [ 1 ]; do
    checksum=$(md5 $watched_files | md5)  # I use Mac so I have `md5`, in linux it's `md5sum`
    if [ "$checksum" != "$previous_checksum" ]; then
        echo "None shall pass!"  # do your stuff here
    fi
    previous_checksum="$checksum"
    sleep 1
done
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
  • +1; a few suggestions: to properly handle filenames with embedded spaces: `watched_files=("$@")`, `md5 "${watched_files[@]}"`; to not falsely report a change in the first iteration: `previous_checksum=`, `if [[ -n $previous_checksum && $checksum != $previous_checksum ]]; then`; to report errors properly: output error messages to _stderr_ with `>&2`, report a non-zero exit code (e.g, `exit 1`). – mklement0 Apr 06 '14 at 03:27
1

This is a nice cli FAM client: http://fileschanged.sourceforge.net/

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Cool library. I forgot to mention that I'm on OSX though, not Linux. It looks like that library is Linux only? – Ross117 Apr 05 '14 at 18:01
  • Well there are build for Linux and BSD which is not surprising. I'd expect that it can be compiled for more or less every unixoid system, so OSX should not be a big problem. The question is: is there a usable FAM available on OSX? – arkascha Apr 05 '14 at 20:07