running the cmd commands from node.js program -


i want run command "node main.js project1" js file used child_process.exec(command[, options][, callback]) function. worked out successfully.now want run 2 commands "node main.js project1" , "node main.js project2" 1 after other using function. tried following code first command runs.please me in doing so

for(var i=0;i<2;i++)  {     if(i==0)     {        var exec = require('child_process').exec,        child;         child = exec('node main.js project1',         function (error, stdout, stderr) {           console.log('stdout: ' + stdout);          //console.log('stderr: ' + stderr);           if (error !== null) {           console.log('exec error: ' + error);            }             });          }       if(i==1)       {        var exec = require('child_process').exec,         child;        child = exec('node main.js project2',        function (error, stdout, stderr) {         console.log('stdout: ' + stdout);         //console.log('stderr: ' + stderr);          if (error !== null) {          console.log('exec error: ' + error);       });     } } 

you have 2 choices ...

first choice use npm module commander npm commander

https://www.npmjs.com/package/commander

the other choice continue current approach.

note ...

javascript not have block scope. variables introduced within block scoped containing function or script

thus declaring var exec twice redefining variable.

also not using exec callback ...

do ...

   var exec = require('child_process').exec,    child;   // function defined here callback function , executed after run command - want run second command inside callback    child = exec('node main.js project1',     function (error, stdout, stderr) {       console.log('stdout: ' + stdout);      //console.log('stderr: ' + stderr);       if (error !== null) {       console.log('exec error: ' + error);       var exec2 = require('child_process').exec,     child;    child = exec2('node main.js project2',    function (error, stdout, stderr) {     console.log('stdout: ' + stdout);     //console.log('stderr: ' + stderr);      if (error !== null) {      console.log('exec error: ' + error);   });            }         });  

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -