0

So I have python script called main.py

#!/usr/bin/env python3
print("hello")
exit (12)

I am trying to capture the exit code via nodejs

const cp = require('child_process');
 cp.exec('python main.py', (er, stdout, stderr)  => {
                console.log(stdout)            
     }).on('close', (code) => {
        console.log(code)    
     }); 

This works and outputs "hello" and "12". But I need to execute python script via another cmd unit. I have tried the code below

const cp = require('child_process');
cp.exec('start cmd /k python ./main.py', (er, stdout, stderr)  => {
                 console.log(stdout)            
    }).on('close', (code) => {
       console.log(code)    
    }); 

But this outputs "hello" and "0" instead of "12". I have been trying to figure this out for past couple hours but not getting anywhere. What am I missing here?

Calling a another child process is not solution for me, since I am also trying to run the python script on a development unit instead on cmd

  • Closely related: https://stackoverflow.com/questions/28318643/getting-the-exit-code-of-an-application-started-with-the-cmd-and-start-comma I don't think you can set the exit code of `cmd.exe` (which is what you're looking at), but you can echo it to stdout and read it from your child process's output, or write it to a file and read the file. – T.J. Crowder Feb 17 '22 at 16:30
  • I am trying to get the exit code of the python script, which is running in a child process on cmd unit – Sharvari Bhosale Feb 18 '22 at 07:11
  • Yes, that was clear from the question. What about my comment made it seem like it wasn't? – T.J. Crowder Feb 18 '22 at 07:13

0 Answers0