Why nodejs spawn process become <defunct> until parent exited
<p>Let's see the phenomenon first,</p>

<p>Nodejs code:</p>

<pre><code>const cp = require('child_process');

var ls = cp.spawn('ls', ['/']);

ls.stdout.on('data', (data) =&gt; {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) =&gt; {
  console.log(`stderr: ${data}`);
});

ls.on('close', (code) =&gt; {
  console.log(`child process closed with code ${code}`);
});

while(true){}
</code></pre>

<p>Run this nodejs code, nothing displayed, seems no event been triggered.</p>

<p>Then run "ps -ef | grep ls | grpe -v grep" in another shell, result is:</p>

<pre><code>liyuanq+ 10995 10990  0 11:06 pts/3    00:00:00 [ls] &lt;defunct&gt;
</code></pre>

<p>If remove the code:</p>

<pre><code>while(true){}
</code></pre>

<p>the node process exit, and triggered the on data event.</p>

<p>The problem is why node not close the spawned process when it actually finished it's job until the parent node process exited.</p>

<p>My environment:</p>

<p>OS: Debian 8.4 x86_64</p>

<p>Node: v6.1.0</p>