Linux tee command occasionally fails if followed by a pipe
<p>I run a find command with tee log and xargs process output; by accident I forget add <code>xargs</code> in second pipe and found this question.</p>

<p>The example:</p>

<pre><code>% tree
.
├── a.sh
└── home
    └── localdir
        ├── abc_3
        ├── abc_6
        ├── mydir_1
        ├── mydir_2
        └── mydir_3

7 directories, 1 file
</code></pre>

<p>and the content of <code>a.sh</code> is:</p>

<pre><code>% cat a.sh
#!/bin/bash
LOG="/tmp/abc.log"

find home/localdir -name "mydir*" -type d  -print | tee $LOG | echo
</code></pre>

<p>If I add the second pipe with some command, such as <code>echo</code> or <code>ls</code>, the write log action would occasionally fail.</p>

<p>These are some examples when I ran <code>./a.sh</code> many times:</p>

<pre><code>% bash -x ./a.sh; cat /tmp/abc.log  // this tee failed
+ LOG=/tmp/abc.log
+ find home/localdir -name 'mydir*' -type d -print
+ tee /tmp/abc.log
+ echo


% bash -x ./a.sh; cat /tmp/abc.log  // this tee ok
+ LOG=/tmp/abc.log
+ find home/localdir -name 'mydir*' -type d -print
+ tee /tmp/abc.log
+ echo

home/localdir/mydir_2  // this is cat /tmp/abc.log output
home/localdir/mydir_3
home/localdir/mydir_1
</code></pre>

<p>Why is it that if I add a second pipe with some command (and forget <code>xargs</code>), the <code>tee</code> command will fail occasionally?</p>