Ivan Skorokhodov Ivan Skorokhodov - 2 months ago 11x
Javascript Question

How to run interactive shell command inside node.js?

I have to run some interactive shell command inside node.js. Lets our interactive shell be

$ python

var cp = require('child_process');
var pythonChildProcess = cp.spawn('python');

pythonChildProcess.stdout.on("data", function(data) {
console.log('data successfully written!', data); // never outputs anything

pythonChildProcess.stdin.write('1 + 1');

This code does not output anything (but stdout should be

But if it would, there will be another problem: how to make it interactive? The process ends when I call
! But I just wanted to end stdin and write next stdin!

If I could emulate pressing of
button - I would be able to interactively write to the process. But adding
to the end of the input string does not help.


First and foremost, one of the things preventing node from interfacing with other interactive shells is that the child application must keep its "interactive" behavior, even when stdin doesn't look like a terminal. python here knew that its stdin wasn't a terminal, so it refused to work. This can be overridden by adding the -i flag to the python command.

Second, as you well mentioned in the update, you forgot to write a new line character to the stream, so the program behaved as if the user didn't press Enter. Yes, this is the right way to go, but the lack of an interactive mode prevented you from retrieving any results.

Here's something you can do to send multiple inputs to the interactive shell, while still being able to retrieve each result one by one. This code will be resistant to lengthy outputs, accumulating them until a full line is received before performing another instruction. Multiple instructions can be performed at a time as well, which may be preferable if they don't depend on the parent process' state. Feel free to experiment with other asynchronous structures to fulfil your goal.

var cp = require('child_process');
var childProcess = cp.spawn('python', ['-i']);


var k = 0;
var data_line = '';

childProcess.stdout.on("data", function(data) {
  data_line += data;
  if (data_line[data_line.length-1] == '\n') {
    // we've got new data (assuming each individual output ends with '\n')
    var res = parseFloat(data_line);
    data_line = ''; // reset the line of data

    console.log('Result #', k, ': ', res);

    // do something else now
    if (k < 5) {
      // double the previous result
      childProcess.stdin.write('2 * + ' + res + '\n');
    } else {
      // that's enough

childProcess.stdin.write('1 + 0\n');