I have code to spawn child process like this:
const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr'], {
detached: true
});
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls..on('error', (data) => {
console.log(`error: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Till now, I can console message from stdout and stderr, and it exited automatically after finish the task.
My question is:
in what situation, it will go to 'error' event?
is it possible that child process can't terminate? If so, when could that happen?
if case 2 happens, how can I kill these child process when my main process already terminated?