title: inotify + stdout piping - output being lost in a pipe
body: <p>I have some one-liner generating events for inotify. </p>
  
  <pre><code>while true; do for i in $(seq 1 100); do touch /tmp/ino/foo$i; sleep 1s; done; rm /tmp/ino/foo*; done
  </code></pre>
  
  <p>I then set up a small bash pipeline to watch that folder, ignoring events about ISDIR (maybe I could do that with inotifywait, but that's not relevant):</p>
  
  <pre><code>inotifywait -m -e close /tmp/ino 2&gt;/dev/null | grep -v ISDIR
  </code></pre>
  
  <p>And that works fine, I see lines like <code>/tmp/ino/ CLOSE_WRITE,CLOSE foo57</code>.</p>
  
  <p>But if I add an extra pipe at the end, I don't get any output. To keep it simple, let's use the fact that <code>grep pattern</code> is idempotent.</p>
  
  <pre><code>inotifywait -m -e close /tmp/ino 2&gt;/dev/null | grep -v ISDIR | grep -v ISDIR
  </code></pre>
  
  <p>This produces <em>no</em> output. I know the my generator is still running, and a pipeless <code>inotifywait -m -e close /tmp/ino</code> in another terminal is still producing output.</p>
  
  <p>After a bit of thinking, I assumed it was probably a buffering problem (issues like this often seem to be). I changed my pipeline to </p>
  
  <pre><code>inotifywait -m -e close /tmp/ino 2&gt;/dev/null | grep -v ISDIR --line-buffered | grep -v ISDIR
  </code></pre>
  
  <p>And now I'm getting output again, so that fixes the problem.</p>
  
  <p>However, I don't really understand why it failed to work without forcing line buffering. I've never experienced issues like this with grep, even with 'slow producing' outputs.
  However, some other programs in the pipeline, forcing <code>sed</code> to be <code>sed -u</code> , and forcing me to add <code>fflush()</code> in at the end of each <code>awk</code> statement.</p>
  
  <p>So, what's forcing strange buffering here, and how can I fix it (without having to scrabble around in man pages looking for esoteric force line buffering commands)?</p>