1

I wrote a function, which runs external commands and captures their outputs, something like this

    proc = subprocess.run(command_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if proc.returncode != 0:

Unfortunately, I don't see long commands output as they running. How to do this conditionally, for example, if loglevel is debug or higher?

Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

0

This code will print the command output as soon as it is received:

import sys
import subprocess
try:
    proc = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        shell=shell)
except OSError as e:
    print('Error executing command', command, e)
    raise
for stdout_line in iter(proc.stdout.readline, b''):
    sys.stdout.write(stdout_line)
    sys.stdout.flush()
proc.stdout.close()
ababak
  • 1,685
  • 1
  • 11
  • 23