Listening to stdout from the Linux console with python3

Asked

Viewed 45 times

1

I’m trying to make a script in python that I need to hear the stdout linux console. The script is to test serial transmission error rate for image recording of an OS on an iMX collibri card, as there are some inconsistencies in serial TX/RX recording, mainly of timeout.

The script works perfectly, however, I found no way to hear the stdout console without running a file/script or process using the module subprocess python with the function Popen(). The code that captures stdout from a file is as follows: cmd the exit of a echo or script/arquivo :

import subprocess
import sys


def run(cmd):
    proc = subprocess.Popen(cmd,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            )
    stdout, stderr = proc.communicate()

    return proc.returncode, stdout, stderr


code, out, err = run([sys.executable, 'run.py'])

print("out: '{}'".format(out))
print("err: '{}'".format(err))
print("exit: {}".format(code))

What I need to do is listen to stdout from the console at runtime, without having to run something before - script/file - to generate an stdout output. But I don’t know if it’s possible to do that.

Thanks!

  • 1

    It seems to me a confusion of concepts, if I understand correctly. Each process has a stdin, stderr and stdout, there is no point to think of listening to a stdout without process. Just as there is no "linux console" and each open console is managed by an independent process (either by Getty or on the specific system). It would be the case of Py open the recording process, to be able to follow (again, if I understood the idea, It was a bit confusing the post, I think). Alternatively your script could open a pipe and on another console you redirect stdout to that pipe, but I think it complicates for nothing.

  • 1

    remembering still that you have the tee command line if you need to split an output to more than one destination

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.