Execute bash command in Node.js and get exit code

后端 未结 4 975
忘了有多久
忘了有多久 2020-12-13 02:02

I can run a bash command in node.js like so:

var sys = require(\'sys\')
var exec = require(\'child_process\').exec;

function puts(error, stdout, stderr) { s         


        
4条回答
  •  不思量自难忘°
    2020-12-13 02:52

    Those 2 commands are running in separate shells.

    To get the code, you should be able to check err.code in your callback.

    If that doesn't work, you need to add an exit event handler

    e.g.

    dir = exec("ls -la", function(err, stdout, stderr) {
      if (err) {
        // should have err.code here?  
      }
      console.log(stdout);
    });
    
    dir.on('exit', function (code) {
      // exit code is code
    });
    

提交回复
热议问题