pythonsubprocessdefunct

Detecting if a process exits cleanly with Python subprocess


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.


Solution

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