47993

Get return value from shell command in python

Question:

I'm doing os.system to tail for a live file and grep for a string How can I execute something when the grep succeeds? For example

cmd= os.system(tail -f file.log | grep -i abc) if (cmd): #Do something and continue tail

Is there any way I can do this? It will only come to the if block when the os.system statement is completed.

Answer1:

You can use subprocess.Popen and read lines from stdout:

import subprocess def tail(filename): process = subprocess.Popen(['tail', '-F', filename], stdout=subprocess.PIPE) while True: line = process.stdout.readline() if not line: process.terminate() return yield line

For example:

for line in tail('test.log'): if line.startswith('error'): print('Error:', line)

Answer2:

<ol><li>

I am not sure that you really need to do this in python - perhaps it would e easier to pipe the tail-f output into awk: <a href="https://superuser.com/questions/742238/piping-tail-f-into-awk" rel="nofollow">https://superuser.com/questions/742238/piping-tail-f-into-awk</a>

</li> <li>

If you want to work in python (because you need to do some processing afterwards) then check this link on how to use tail -f: <a href="https://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python" rel="nofollow">How can I tail a log file in Python?</a>

</li> </ol>

Recommend

  • python subprocess use cmd for print() commands
  • UpdateException when using SQL Server Compact with Entity Framework
  • SyntaxError: Unexpected token ' in JSON at position 1
  • Cassandra eats memory
  • Get process output without blocking
  • Need to display iframe when link is clicked from menu
  • Getting p-values from leave-one-out in R
  • Vim syntax highlighting
  • AWS: Lambda function cannot call rest api using private API of EC2 instance
  • In Ember.js, what's the difference between store save and store commit?
  • Python subprocess communicate kills my process
  • Why can't pass only 1 coulmn to glmnet when it is possible in glm function in R?
  • Matlab Generating a Matrix
  • How can I communicate between two python scripts?
  • Open Video file in VLC through Python
  • How to fork and exec a server and wait until it's ready?
  • os.system not working, but typing the same thing into the command prompt works
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • Programmatically check if PHP is installed using Python
  • Downloading files from Google Storage using Spark (Python) and Dataproc
  • subprocess.Popen('start') fails
  • How to capture enclosing scope in Runnable
  • How to replace TouchesBegan with UIGestureRecognizer
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • How to 'create temp table as select' in Slick?
  • SyntaxError: (irb):26: both block arg and actual block given
  • MySQL Order by column = x, column asc?
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Python CGI os.system causing malformed header
  • Handling un-mapped Rest path
  • Weird JavaScript statement, what does it mean?
  • SQL merge duplicate rows and join values that are different
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory