Andre Dixon
Andre Dixon

Reputation: 360

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.

Upvotes: 3

Views: 1321

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

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)

Upvotes: 1

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83818

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

Upvotes: 2

Related Questions