0

I want to run a command like this

grep -w 1 pattern <(tail -f mylogfile.log)

basically from a python script i want to monitor a log file for a specific string and continue with the python script as soon as i found that.
I am using os.system(), but that is hanging. The same command in bash works good.

I have a very old version of python (v2.3) and so don't have sub-process module.

do we have a way to acheive this

avasal
  • 14,350
  • 4
  • 31
  • 47
puneet agrawal
  • 343
  • 2
  • 4
  • 13
  • You could try the `commands` module, but at least one person should give to simple answer -- upgrade to a newer version of python ;) – mgilson Jul 30 '12 at 12:29
  • presumably he has the same problem upgrading python as he does pulling [`subprocess.py` from SVN](http://svn.python.org/view/%2acheckout%2a/python/tags/r255/Lib/subprocess.py) – Mike Pennington Jul 30 '12 at 12:34

3 Answers3

1

In Python 2.3, you need to use subprocess from SVN

import subprocess
import shlex
subprocess.call(shlex.split("/bin/bash -c 'grep -w 1 pattern <(tail -f mylogfile.log)'"))

To be explicit, you need to install it from the SVN link above.

You need to call this with /bin/bash -c due to the shell redirection you're using


EDIT

If you want to solve this with os.system(), just wrap the command in /bin/bash -c since you're using shell redirection...

os.system("/bin/bash -c 'grep -w 1 pattern <(tail -f mylogfile.log)'")
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • again, i don't have subprocess and can't download any new modules in my current enviroment :-( – puneet agrawal Jul 30 '12 at 12:00
  • Not being able to download is new information, that would have been nice to know... although I wonder how hard it is to copy `subprocess.py` into a file in `vi` since you're presumably accessing via `ssh` – Mike Pennington Jul 30 '12 at 12:01
0

First of all, the command i think you should be using is grep -w -m 1 'pattern' <(tail -f in)

For executing commands in python, use the Popen constructor from the subprocess module. Read more at http://docs.python.org/library/subprocess.html

nims
  • 3,751
  • 1
  • 23
  • 27
-1

If I understand correctly, you want to send the output to python like this -

tail -f mylogfile.log | grep -w 1 pattern | python yourscript.py

i.e., read all updates to the log file, and send matching lines to your script.

To read from standard input, you can use the file-like object: sys.stdin.

So your script would look like

import sys

for line in sys.stdin.readlines():
    #process each line here.
Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35