Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top