Skip to content

Instantly share code, notes, and snippets.

@mnyamor
Created October 30, 2016 22:50
Show Gist options
  • Save mnyamor/32d7f4bafc6b59bdd4aaf649a849fb6d to your computer and use it in GitHub Desktop.
Save mnyamor/32d7f4bafc6b59bdd4aaf649a849fb6d to your computer and use it in GitHub Desktop.
Creating Child Process With Spawn
/*alwaysTalking.js
var sayings = [
"You may delay, but time will not.",
"Tell me and I forget. Teach me and I remember. Involve me and I learn.",
"It takes many good deeds to build a good reputation, and only one bad one to lose it.",
"Early to bed and early to rise makes a man healthy, wealthy and wise.",
"By failing to prepare, you are preparing to fail.",
"An investment in knowledge pays the best interest.",
"Well done is better than well said."
];
var interval = setInterval(function() {
var i = Math.floor(Math.random() * sayings.length);
process.stdout.write(`${sayings[i]} \n`);
}, 1000);
process.stdin.on('data', function(data) {
console.log(`STDIN Data Recieved -> ${data.toString().trim()}`);
clearInterval(interval);
process.exit();
});
*/
var spawn = require("child_process").spawn;
var cp = spawn("node", ["alwaysTalking"]);
cp.stdout.on("data", function(data) {
console.log(`STDOUT: ${data.toString()}`);
});
cp.on("close", function() {
console.log("Child Process has ended");
process.exit();
});
setTimeout(function() {
cp.stdin.write("stop");
}, 4000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment