Я хочу порождать ребенка, и через 5 секунд я хочу убить этого ребенка. В детстве я хочу распечатать пока до выхода.
Мой код не работает, пока не напечатан, почему?
Родитель:
const child = require('child_process').spawn('node', ['index.js']);
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
child.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
child.on('exit', function (code, signal) {
console.log('child process exited with ' +
`code ${code} and signal ${signal}`);
});
setTimeout(() => {
child.kill('SIGINT');
}, 5000)
Child
const handleAppExit = () => {
process.stdin.resume()//so the program will not close instantly
const exitHandler = (options, err) => {
console.log('bye bye')
process.exit()
}
// do something when app is closing
process.on('exit', exitHandler.bind(null, { cleanup: true, }))
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, { exit: true }))
process.on('SIGTERM', exitHandler.bind(null, { cleanup: true, exit: true }))
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }))
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))
}
handleAppExit()
// some other async code - require('http').createServer(require('express')()) ...
Итак, когда я вручную запускаю дочерний узел, используя узел index.js
, и после запуска я нажимаю ctrl c, тогда прощай печатается. Почему не работает с детской комбинацией?
Спасибо за любую помощь.