I am implementing a CI platform and need to run a bunch of tests in a shell script that need to be delivered and analyzed in real-time. I figured I needed to use subprocess.Popen
and use .stdout.readline()
to get the output upon execution. A sample of that is demonstrated below:
process = subprocess.Popen(["/bin/bash", "script.sh"], stdout=subprocess.PIPE)
while True:
output = process.stdout.readline()
if process.poll is not None and output == "":
break
if output:
# do whatever you want with the output
print(output.strip())
The above code works, however, during the course of execution, there maybe errors. I would have to use process.stderr.readline()
to get the errors but I want to be able to get the errors and output as a single object (string) not separate objects (strings).