How to get exit code when using Python subprocess communicate method?

318,984

Solution 1

Popen.communicate will set the returncode attribute when it's done(*). Here's the relevant documentation section:

Popen.returncode 
  The child return code, set by poll() and wait() (and indirectly by communicate()). 
  A None value indicates that the process hasn’t terminated yet.

  A negative value -N indicates that the child was terminated by signal N (Unix only).

So you can just do (I didn't test it but it should work):

import subprocess as sp
child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
streamdata = child.communicate()[0]
rc = child.returncode

(*) This happens because of the way it's implemented: after setting up threads to read the child's streams, it just calls wait.

Solution 2

.poll() will update the return code.

Try

child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
returnCode = child.poll()

In addition, after .poll() is called the return code is available in the object as child.returncode.

Solution 3

You should first make sure that the process has completed running and the return code has been read out using the .wait method. This will return the code. If you want access to it later, it's stored as .returncode in the Popen object.

Solution 4

exitcode = data.wait(). The child process will be blocked If it writes to standard output/error, and/or reads from standard input, and there are no peers.

Solution 5

Use process.wait() after you call process.communicate().
For example:

import subprocess

process = subprocess.Popen(['ipconfig', '/all'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
exit_code = process.wait()
print(stdout, stderr, exit_code)
Share:
318,984

Related videos on Youtube

CarpeNoctem
Author by

CarpeNoctem

Updated on February 24, 2022

Comments

  • CarpeNoctem
    CarpeNoctem about 2 years

    How do I retrieve the exit code when using Python's subprocess module and the communicate() method?

    Relevant code:

    import subprocess as sp
    data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0]
    

    Should I be doing this another way?

  • Mechanical snail
    Mechanical snail over 10 years
    .communicate() already waits for the subprocess to terminate.
  • uglycoyote
    uglycoyote over 7 years
    This example helped me, but it would be nice if examples didn't do the "import subprocess as sp" pattern of importing something standard as an obscure abbreviation. While this trims 8 characters off the code that follows it, it also makes it difficult to understand and reuse.
  • Jason C
    Jason C over 7 years
    @uglycoyote There's no rule that says you have to copy and paste. Just retype it however you want, it's like 4 like lines.
  • Mitch
    Mitch over 7 years
    @uglycoyote you could also edit it to be something like from subprocess import Popen and then just use Popen instead of subprocess(or sp).Popen which I'd say probably increases readability and shortens lines
  • WesternGun
    WesternGun over 6 years
    Yeah... must call process.communicate() and then assign returncode to some variable. If the assignment is done before calling communicate, is None.
  • Nisba
    Nisba about 6 years
    Is it possible to show the return code without redirecting the pipe? I am calling a bash code and I would like to see the output in real time in the terminal
  • NateW
    NateW over 5 years
    when I did this .poll() was empty. I had to run child.communicate() in the line above child.poll() for this to work.
  • gg99
    gg99 about 5 years
    I think you meant to use .wait() instead of .poll(), as per documentation: docs.python.org/3/library/subprocess.html. Note that .wait() takes an optional timeout param which can be convenient.
  • Ryan McGrath
    Ryan McGrath over 2 years
    @Nisba, the redirection is not required.
  • tripleee
    tripleee about 2 years
    This is an adaptation of my answer to a duplicate question.
  • tripleee
    tripleee about 2 years
    You can't communicate after wait.
  • tripleee
    tripleee about 2 years
    You can't meaningfully wait more than once.