title: How can I interrupt or debounce an inotifywait loop?
body: <p>I have a little script that <a href="https://askubuntu.com/questions/385333/how-do-i-get-the-filename-from-inotifywait-events">watches files for changes using <code>inotifywait</code></a>. When something changes, a batch of files are sent through a process (compiled, compressed, reorganised, etc) that takes about ten seconds to run.</p>
  
  <p>Consider the following example:</p>
  
  <pre><code>touch oli-test
  inotifywait -mq oli-test | while read EV; do sleep 5; echo "$EV"; done
  </code></pre>
  
  <p>If you run <code>touch oli-test</code> in another terminal a few times, you'll see that each loop completes before it moves on. That scenario is very real to me. If I forget to save a file while it's already processing, or notice a mistake, the events stack up and I'm waiting minutes.</p>
  
  <p>It strikes me that there are two techniques that would make this workflow objectively better. I'm not sure what is easiest or best, so I'm presenting both:</p>
  
  <ol>
  <li><p><strong>Interrupt</strong> previous run-throughs, and restart immediately. The scripted process is currently just an inline set of commands. I could break them out to Bash functions, I'm not wild about breaking them further out than that.</p></li>
  <li><p><strong>Debounce</strong> the list of things waiting to be processed so that if five events happen at once (or while it's already processing), it only runs once more. </p></li>
  </ol>
  
  <p>(Or both... because I'm certain there are cases where both would be useful)</p>
  
  <p>I am also open to approaches that are different from <code>inotifywait</code> but they need to give me the same outcome and work on Ubuntu.</p>