Making lftp write to stdout without having to close the process first

1.3k views Asked by At

I'm trying to wrap the lftp program in a node.js application, using child_process. The problem is that lftp doesn't write its output to stdout, so I cannot catch its output in node.js. Sample code:

var proc = require('child_process').spawn('lftp', ['-p', port, '-u', username + ',' + password, host]);

proc.stdout.on('data', function (data) {
    console.log('stdout:', data.toString('utf-8'));
});

proc.on('exit', function (code) {
    console.log('process exited with code ' + code);
});

proc.stdin.write('ls');
// proc.stdin.end();

If I uncomment the line that calls stdin.end() for the lftp child process, the output from the ls command appears in my terminal as it should. If I don't the process simply hangs and nothing gets outputted.

I've also tried using unbuffer, but it doesn't seem to allow me to write to lftp's stdin anymore. It outputs the usual "[Resolving host address...]" stuff, but not the output from the ls command.

My question is: what do I have to do to be able to interact with lftp using node.js' child_process?

1

There are 1 answers

0
Victor Stanciu On BEST ANSWER

Well, this was dumb. I forgot to write a newline after the ls command to stdin. It seems to work without the need for unbuffer.