Search code examples
javascriptnode.jsreturn-valuechild-processexit-code

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'.


Solution

  • 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)