0

I am running a python on Linux terminal and it requires some bash commands to run while the test is running. So, I am using the subprocess module and running my test commands (bash script). These so-called bash commands might print something on the CLI which I need to know if it does while I am running my python code in parallel.

for Ex :

# running my python TCP server 

subprocess.call(['.\run_some_shell_commands.sh'],shell=True)

while True:
   # I am doing some other python stuff 

   if (CLI_HAS_SOME_OUTPUT_DETECTED):
       #record the output to some variable 


   # doing some more python stuff 


If I know for sure that run_some_shell_commands.sh returns some output for sure, I could simply use A = subprocess.checkoutput(['.\run_some_shell_commands.sh'],shell=True) which would save its output in variable A ..

Is there any way to grab the last n lines of the terminal ?? so that I can check if that event has occurred and I can assign that to CLI_HAS_SOME_OUTPUT_DETECTED

Any suggestions are highly appreciated.

Saira

9
  • You're going to need to capture stdout or stderr, which you can do with capture_output=True. Commented Feb 2, 2020 at 16:25
  • @AlexanderHuszagh Apologies for my late reply.. was trying out different stuff with capture_output .. Want to clarify onething.. the OUTPUT that I am expecting is not from the shell script I'm running.. As far as I understood from the focs , capture_output saves the output of whatever shell commands we are trying to execute .. Commented Feb 3, 2020 at 16:03
  • In such case where I am not sure of when that event might happen , I might have to continuously monitor the terminal output for some threshold time and if that happens, I might need to save the text it is printing .. thought of using script command , but the linux box is properitary and is not letting me install any packages Commented Feb 3, 2020 at 16:11
  • You can run an ongoing script and pipe the output to something else, where you can continually process it. Think carefully about what you need to do, I deleted my original answer because it might have answered your exact use-case. Commented Feb 3, 2020 at 16:16
  • I tried something like this and it does the job for me (as of now, not sure how good this approach is though ) Commented Feb 3, 2020 at 16:52

2 Answers 2

1
import subprocess
import time as t
cmd = [' ']
P = subprocess.check_output(cmd,shell=True)
while True :
        print(P)
        t.sleep(0.1)
Sign up to request clarification or add additional context in comments.

Comments

0

This is answered in Running shell command and capturing the output. There are two classes of shell commands, executables and inbuilt commands, in some programming languages this can make a difference, see How do I listen for a response from shell command in android studio?

1 Comment

thanks ralf.. I think the point was to use the capture_output (in py3+)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.