Skip to content

Instantly share code, notes, and snippets.

@ayal
Last active November 3, 2020 16:38
Show Gist options
  • Save ayal/a27ce413088393ce26ead3ed0a96765b to your computer and use it in GitHub Desktop.
Save ayal/a27ce413088393ce26ead3ed0a96765b to your computer and use it in GitHub Desktop.
killing forked children after setrawmode(true -> false)
var cp = require("child_process");
console.log('hi from child', process.stdin.isRaw)
setInterval(() => {
console.log('hi from child', process.stdin.isRaw);
}, 1000);
var child2 = cp.fork('child2.js', []);
console.log('child 2 pid', child2.pid);
console.log('2 hi from child 2', process.stdin.isRaw)
var handle = setInterval(() => {
console.log('2 hi from child 2', process.stdin.isRaw);
}, 1000);
console.log('start parent');
var cp = require("child_process");
process.stdin.setRawMode(true);
process.stdin.setEncoding('utf8');
const CTRL_C = '\u0003';
console.log('HAY parent', process.stdin.isRaw, process.stdin.isTTY);
var handler = (str) => {
console.log('a keypress!');
if (str === CTRL_C) {
console.log('press ctrl c again to exit');
process.stdin.setRawMode(false);
}
}
process.stdin.on('data', handler)
process.on('SIGINT', ()=>{
process.stdin.emit('end') // without this terminal might wont be responsive to ctrl+c after exit
process.exit();
})
var child = cp.fork('child.js', []);
console.log('child pid', child.pid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment