3

I wrote a python script that's responsible for executing other scripts. I do notice that every now and then, I get a defunct process.

I was reading some other threads on StackOverFlow and they said that you can catch a problem if .communicate or .call aren't used.

The only thing I'm struggling with now is that I need to also get the exit code of the process.

My code is below.

job.append("py")
job.append("\tmp\test\sleep.py")
job.append("20")
p = subprocess.Popen(job, stderr=subprocess.PIPE,stdout=subprocess.PIPE)
exit_code_main = p.wait()

I need to get all output from the execution of the subprocess and I need to get the error output if there is any. The exit code is what I use to determine if the script in the subprocess executed successfully.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Andre Dixon
  • 330
  • 4
  • 14

2 Answers2

2

Here is an example how to capture stdout, stderr and exit code with subprocess:

    p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()

    logger.info("Executed notification script %s, exit code %d", args, p.returncode)
    logger.info("stdout: %s", stdout)
    logger.info("stderr: %s", stderr)

    if p.returncode != 0:
        raise RuntimeError("The script did not exit cleanly: {}".format(args))
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
1

You can use check_output to catch any non-zero exit status:

from subprocess import check_output, CalledProcessError
try:
    out = check_output(job)
except CalledProcessError as e:
    print(e.returncode)
    print(e.message)
else:
    print(out)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321