title: Having trouble with inotifywait, trying to watch a directory for golang changes
body: <p>I'm trying to write a bash script to monitor a directory for changes. If the file is a .go or .html file I would like to kill a specific process and then start it.</p>
  
  <p>This is failing all over the place and I'm not sure why. I tried my best to get this working after scouring a lot of web sites for help but I can't get it.</p>
  
  <p>The ideal solution is that I would run it by passing a directory and a file to run and it would just reload the process when I save a file.</p>
  
  <p>I am running it like so:</p>
  
  <p><em>./gowatcher /path/to/my/directory/to/watch foo.go</em></p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>#!/usr/bin/env bash
  
  WATCH_DIR=$1
  FILENAME=$2
  
  function restart_goserver() {
    if go run $FILENAME
    then
      pkill -9 -f $FILENAME &gt; /dev/null 2&gt;&amp;1
      pkill -9 -f a.out &gt; /dev/null 2&gt;&amp;1
      go run $FILENAME &amp;
      echo "started $FILENAME"
    else
      echo "server restart failed"
    fi
  }
  
  cd $WATCH_DIR
  restart_goserver
  
  echo "watching directory: $WATCH_DIR"
  inotifywait -mrq -e close_write $WATCH_DIR | while read file
  do
    if grep -E '^(.*\.go)|(.*\.html)$'
    then
      echo "--------------------"
      restart_goserver
    fi
  done
  </code></pre>