Skip to content

Instantly share code, notes, and snippets.

@boopathi
Last active April 14, 2019 17:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boopathi/806ef59c346cb789a21b to your computer and use it in GitHub Desktop.
Save boopathi/806ef59c346cb789a21b to your computer and use it in GitHub Desktop.
File descriptors - stdin and stdout - isTTY ? in a forked process
Case I: node parent
Parent: true From tty: true
Parent: true From tty: true
TTY: true From tty: true
TTY: true From tty: true
Case II: node parent > asdf
Parent: true From tty: true
Parent: undefined From tty: true
TTY: true From tty: true
TTY: undefined From tty: true
Case III: cat asdf | node parent
Parent: undefined From tty: false
Parent: true From tty: false
TTY: undefined From tty: false
TTY: true From tty: false
Case IV: cat asdf | node parent > asdf
Parent: undefined From tty: false
Parent: undefined From tty: false
TTY: undefined From tty: false
TTY: undefined From tty: false
var cp = require('child_process');
var tty = require('tty');
cp.fork('./tty.js');
console.log("Parent: ", process.stdin.isTTY, " From tty: ", tty.isatty(process.stdin));
console.log("Parent: ", process.stdout.isTTY, " From tty: ", tty.isatty(process.stdout));
echo "Case I: node parent"
node parent
echo ""
echo "Case II: node parent > asdf"
node parent > asdf
cat asdf
echo ""
echo "Case III: cat asdf | node parent"
cat asdf | node parent
echo ""
echo "Case IV: cat asdf | node parent > asdf"
cat asdf | node parent > asdf
cat asdf
var tty = require('tty');
console.log("TTY: ", process.stdin.isTTY, " From tty: ", tty.isatty(process.stdin));
console.log("TTY: ", process.stdout.isTTY, " From tty: ", tty.isatty(process.stdout));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment