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

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 … Read more

live output from subprocess command

TLDR for Python 3: import subprocess import sys with open(“test.log”, “wb”) as f: process = subprocess.Popen(your_command, stdout=subprocess.PIPE) for c in iter(lambda: process.stdout.read(1), b””): sys.stdout.buffer.write(c) f.buffer.write(c) You have two ways of doing this, either by creating an iterator from the read or readline functions and do: import subprocess import sys # replace “w” with “wb” for … Read more

Constantly print Subprocess output while process is running

You can use iter to process lines as soon as the command outputs them: lines = iter(fd.readline, “”). Here’s a full example showing a typical use case (thanks to @jfs for helping out): from __future__ import print_function # Only Python 2.x import subprocess def execute(cmd): popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) for stdout_line in iter(popen.stdout.readline, “”): … Read more

read subprocess stdout line by line

I think the problem is with the statement for line in proc.stdout, which reads the entire input before iterating over it. The solution is to use readline() instead: #filters output import subprocess proc = subprocess.Popen([‘python’,’fake_utility.py’],stdout=subprocess.PIPE) while True: line = proc.stdout.readline() if not line: break #the real code does filtering here print “test:”, line.rstrip() Of course … Read more

Retrieving the output of subprocess.call() [duplicate]

If you have Python version >= 2.7, you can use subprocess.check_output which basically does exactly what you want (it returns standard output as string). Simple example (linux version, see note): import subprocess print subprocess.check_output([“ping”, “-c”, “1”, “8.8.8.8”]) Note that the ping command is using linux notation (-c for count). If you try this on Windows … Read more

How do I pass a string into subprocess.Popen (using the stdin argument)?

Popen.communicate() documentation: Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. Replacing os.popen* pipe = os.popen(cmd, ‘w’, bufsize) # ==> pipe = Popen(cmd, shell=True, … Read more

How to use `subprocess` command with pipes

To use a pipe with the subprocess module, you have to pass shell=True. However, this isn’t really advisable for various reasons, not least of which is security. Instead, create the ps and grep processes separately, and pipe the output from one into the other, like so: ps = subprocess.Popen((‘ps’, ‘-A’), stdout=subprocess.PIPE) output = subprocess.check_output((‘grep’, ‘process_name’), … Read more

How to hide output of subprocess

For python >= 3.3, Redirect the output to DEVNULL: import os import subprocess retcode = subprocess.call([‘echo’, ‘foo’], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) For python <3.3, including 2.7 use: FNULL = open(os.devnull, ‘w’) retcode = subprocess.call([‘echo’, ‘foo’], stdout=FNULL, stderr=subprocess.STDOUT) It is effectively the same as running this shell command: retcode = os.system(“echo ‘foo’ &> /dev/null”)

Using module ‘subprocess’ with timeout

In Python 3.3+: from subprocess import STDOUT, check_output output = check_output(cmd, stderr=STDOUT, timeout=seconds) output is a byte string that contains command’s merged stdout, stderr data. check_output raises CalledProcessError on non-zero exit status as specified in the question’s text unlike proc.communicate() method. I’ve removed shell=True because it is often used unnecessarily. You can always add it … Read more