1

Iam trying to execute a terminal command using node.js spawn

for that am using the code

console.log(args)

var child = spawn("hark", args, {cwd: workDir});
        child.stdout.on('data', function(data) {

          console.log(data.toString())        
        });

        child.stderr.on('data', function(data) {
          console.log('stdout: ' + data);

        });

        child.on('close', function(code) {
          console.log('closing code: ' + code);

        });

But it treated greater than>as string">" and getting output as

tshark: Invalid capture filter "> g.xml" 

    That string isn't a valid capture filter (syntax error).
    See the User's Guide for a description of the capture filter syntax.

How can i use > without string

Psl
  • 3,830
  • 16
  • 46
  • 84
  • '> ggggg.xml' is shell syntax for redirecting stdout to file. You should use proper options of spawn http://nodejs.org/api/child_process.html#child_process_options_stdio or use `child_process.exec` which will call shell. – Alexey Ten Feb 10 '15 at 07:51

1 Answers1

2

You can use file streams to put all output from spawned hark to g.xml.
Example:

// don't need ">" in args
var args = [' 02:00:00:00' ,'-s','pdml'],
    logStream = fs.createWriteStream('./xml');

var spawn = require('child_process').spawn,
    child = spawn('tshark', args);

child.stdout.pipe(logStream);
child.stderr.pipe(logStream);

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

Because of child here is a stream, you can just pipe it into your logged file stream.

Psl
  • 3,830
  • 16
  • 46
  • 84
Ruslan Ismagilov
  • 1,360
  • 7
  • 14
  • yes one more thing..ur coding is working..but it did not output the valid xml file....some attributes are missing – Psl Feb 13 '15 at 09:35
  • Try to play with options: http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options – Ruslan Ismagilov Feb 13 '15 at 09:55
  • I will get the full xml file when i manully exit the running terminal using ctrl+C ..but in code i tried to exit the process and killed the child process but not getting the full output in that case also – Psl Feb 13 '15 at 10:13
  • Tried using 'exit' event instead of 'close' for `child`? – Ruslan Ismagilov Feb 13 '15 at 13:59
  • But it does not goes inside 'exit' event or 'close' event..that is the main problem..also i cannot identify the process is completed or not and i quil the terminal by ctrl+c command – Psl Feb 16 '15 at 03:58
  • @Psl if it is still topical, please provide an gist example with real data that reproduce this problem. Past a link on this gist here. I'll try to play with that. – Ruslan Ismagilov Feb 16 '15 at 16:09