4

I'm using the scp command in a node application to transfer a file to a server; I've not been able to show in console the progress while transferring, as "scp" normally does if you use it in the shell manually; here is my code:

//require system modules
var spawn = require('child_process').spawn;

console.log("copy begins...");

var executor = spawn("scp", ["-r", ".tmp/app.tar.gz", "user@server:/home/user"]);

executor.stderr.on('data', function(data) {
    console.log(data.toString());
});

executor.stdout.on('data', function(data) {
    console.log(data.toString());
});

executor.stdout.on('end', function(data) {
    console.log("copied");
});

executor.on('close', function(code) {
    if (code !== 0) {
        console.log('Failed: ' + code);
    }
});

This just logs in the console "copy begins..." and "copied", but nothing else; is it possible to get the output of the scp command during its execution and send it to the console?

Cereal Killer
  • 3,387
  • 10
  • 48
  • 80

1 Answers1

3

scp only outputs what it calls the "progress meter" if its standard output is a TTY:

$ scp localhost:/etc/passwd . 
passwd                                        100% 5253     5.1KB/s   00:00    
$ scp localhost:/etc/passwd . | cat
$ 

So your program would have to arrange for the standard output of the scp process to be a TTY or PTY (pseudo-tty).

I'm not a node programmer so I can't provide you with a complete example of how to do that. You could try running scp through the expect program. It's capable of allocating PTYs and running child processes through them. Alternately, it appears that node has a 'tty' module which might be able to do what you want. This question discusses running vim from node through a tty; you might be able to use the same approach.

Community
  • 1
  • 1
Kenster
  • 23,465
  • 21
  • 80
  • 106
  • Ok, now I understand more; node js have a core module for tty; unfortunally I've not been able until now to get the desired result... – Cereal Killer Apr 11 '15 at 14:54