После многих тестов я нашел, по крайней мере, один способ решения этой проблемы: уничтожить все каналы, прежде чем покинуть основной процесс.
Одна хитрость заключается в том, что дочерний процесс должен правильно обрабатывать трубы, разрушающие, если не может получить ошибку и закрыть в любом случае. В этом примере дочерний процесс узла, кажется, не имеет проблем с этим, но он может отличаться от других сценариев.
main. js
const { spawn } = require('child_process')
console.log('Start Main')
let child = spawn('node', ['child.js'], { detached: true })
child.unref() // With this the main process end after fully disconnect the child
child.stdout.on('data', data => {
console.log(`Got data : ${data}`)
})
// In real case should be triggered just before the end of the main process
setTimeout(() => {
console.log('Disconnect the child')
child.stderr.unpipe()
child.stderr.destroy()
child.stdout.unpipe()
child.stdout.destroy()
child.stdin.end()
child.stdin.destroy()
}, 5000)
child. js
console.log('Start Child')
setInterval(function() {
process.stdout.write('hello from child')
}, 1000)
output
Start Main
Получил данные: Start Child
Получил данные: hello from child
Получил данные: hello from child
Получил данные: привет от ребенка
Получил данные: привет от ребенка
Отключил ребенка