简体   繁体   中英

How to get the exit code from a file ran using javascript child_process.execFile

Here is my python code:

#!/bin/python
import sys
sys.exit(4)

Here is my javascript

var exec = require('child_process').execFile
exec('./script.py', (err, data) => { if(err) { console.log(err.status) } })

But it doesn't work because there is not such thing as err.status what I want to have in my console logs is '4'.

There isn't err.status , but there is err.code .

This should work:

var exec = require('child_process').execFile
exec('./script.py', (err, data) => { if(err) { console.log(err.code) } })

There is also err.signal and err.killed

From the nodejs docs:

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.

( https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback )

execFile 's callback get 3 arguments:

  • error , if there was.
  • stdout , the stdout of process.
  • stderr , the stderr of the process.

So, by checking the stderr , you should be able to achieve the expected result:

var exec = require('child_process').execFile
exec('./script.py',(err,stdout,stderr)=>{console.log(stderr)})

See this in the Node.js docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM