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

Saira
  • 111
  • 2
  • 9
  • You're going to need to capture stdout or stderr, which you can do with `capture_output=True`. – Alex Huszagh Feb 02 '20 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 .. – Saira Feb 03 '20 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 – Saira Feb 03 '20 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. – Alex Huszagh Feb 03 '20 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 ) – Saira Feb 03 '20 at 16:52
  • ``` import subprocess import time as t cmd = [' '] P = subprocess.check_output(cmd,shell=True) while True : print(P) t.sleep(0.1) ``` – Saira Feb 03 '20 at 16:53
  • Thanks @AlexanderHuszagh – Saira Feb 03 '20 at 16:55
  • That's an okay solution, and yes, it should work. I would likely use `subprocess.run` with `capture_output` set, and then pass `stdout` and `stderr` separately, and then do something with those variables (do your conditional logic). – Alex Huszagh Feb 03 '20 at 17:02
  • You can set `RADAR = False` as a global variable, then update `RADAR` on each iteration (remember to add `global RADAR` in any function you set a global variable). I'd likely run the script a lot less frequently, and just check in much slower intervals. – Alex Huszagh Feb 03 '20 at 17:03

2 Answers2

1
import subprocess
import time as t
cmd = [' ']
P = subprocess.check_output(cmd,shell=True)
while True :
        print(P)
        t.sleep(0.1)
Saira
  • 111
  • 2
  • 9
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?

ralf htp
  • 9,149
  • 4
  • 22
  • 34