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

Posted on

Question :

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

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?

Answer #1:

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

  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.

Answered By: Eli Bendersky

Answer #2:

.poll() will update the return code.


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.

Answered By: Matthew Vernon

Answer #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.

Answered By: Noufal Ibrahim

Answer #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.

Answered By: khachik

Answer #5:

This worked for me. It also prints the output returned by the child process

child = subprocess.Popen(serial_script_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    retValRunJobsSerialScript = 0
    for line in child.stdout.readlines():
        print line           
    retValRunJobsSerialScript= child.returncode
Answered By: Chinni Mahesh

Answer #6:

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)
Answered By: Jossef Harush

Answer #7:

Please see the comments.


import subprocess

class MyLibrary(object):

    def execute(self, cmd):
        return subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True,)
    def list(self):
        command = ["ping", ""]
        sp = self.execute(command)
        status = sp.wait()  # will wait for sp to finish
        out, err = sp.communicate()
        return status # 0 is success else error

test = MyLibrary()




Pinging [] with 32 bytes of data:
Reply from bytes=32 time=108ms TTL=116
Reply from bytes=32 time=224ms TTL=116
Reply from bytes=32 time=84ms TTL=116
Reply from bytes=32 time=139ms TTL=116

Ping statistics for
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 84ms, Maximum = 224ms, Average = 138ms

Answered By: Aaj Kaal

Leave a Reply

Your email address will not be published. Required fields are marked *