Let's see the phenomenon first,
Nodejs code:
const cp = require('child_process');
var ls = cp.spawn('ls', ['/']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process closed with code ${code}`);
});
while(true){}
Run this nodejs code, nothing displayed, seems no event been triggered.
Then run "ps -ef | grep ls | grpe -v grep" in another shell, result is:
liyuanq+ 10995 10990 0 11:06 pts/3 00:00:00 [ls] <defunct>
If remove the code:
while(true){}
the node process exit, and triggered the on data event.
The problem is why node not close the spawned process when it actually finished it's job until the parent node process exited.
My environment:
OS: Debian 8.4 x86_64
Node: v6.1.0