title: How do I listen and spawn multiple child process in nodejs
body: <p>I am trying to execute the same java jar file as a child process in nodejs in a for loop, but how do I listen to multiple child output???</p>
  
  <p>The test.jar file runs in an infinite loop and all it does is increment and print the number. </p>
  
  <p>Below is my code, and it does spawn multiple child process in nodejs, but it only print the pid for the last child, and the content from other childs are all added to the last child.</p>
  
  <pre><code>var exec = require('child_process').exec, child;
  var state = "java -jar " + "c://test.jar "
  var exec = require('child_process').exec;
  
  for (var i = 0; i &lt; 10; i++) {
  
      var child = exec(state);
  
      // Add the child process to the list for tracking
      p_list.push({process:child, content:""});
  
      // Listen for any response:
      child.stdout.on('data', function (data) {
          console.log(child.pid, data);
          p_list[i].content += data;
      });
  
      // Listen for any errors:
      child.stderr.on('data', function (data) {
          console.log(child.pid, data);
          p_list[i].content += data;
      }); 
  
      // Listen if the process closed
      child.on('close', function(exit_code) {
          console.log('Closed before stop: Closing code: ', exit_code);
      });
  }
  </code></pre>