Execute bash command in Node.js and get exit code

后端 未结 4 982
忘了有多久
忘了有多久 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:48

    From the docs:

    If a callback function is provided, it is called with the arguments (error, stdout, stderr). On success, error will be null. On error, error will be an instance of Error. The error.code property will be the exit code of the child process while error.signal will be set to the signal that terminated the process. Any exit code other than 0 is considered to be an error.

    So:

    exec('...', function(error, stdout, stderr) {
      if (error) {
        console.log(error.code);
      }
    });
    

    Should work.

提交回复
热议问题