0

How do I run this linux command in Python 2.7?

tail -f *file.log | grep 10.10.10.100

The below doesn't seem to observe the grep. I saw previous posts but did not see anyone trying to tail a file then pipe to grep.

subprocess.call(["tail", "-f", "*files.log", "|", "grep", "10.10.10.100"])
Cyrus
  • 69,405
  • 13
  • 65
  • 117
juanald_reagan
  • 195
  • 2
  • 12
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – pvg Aug 26 '17 at 13:37
  • @pvg - I reviewed that thread and it doesn't answer my question...I know how to make the call just not in this context. – juanald_reagan Aug 26 '17 at 13:38
  • @user1670178 please review it again. It covers shell parsing, which is what you're missing, in multiple ways. It's an exact dupe and there are others similar. – pvg Aug 26 '17 at 13:40
  • And here's another one. As I said, this question is many similar are answered extensively on the site, pick your dupe. https://stackoverflow.com/questions/13332268/python-subprocess-command-with-pipe – pvg Aug 26 '17 at 13:45
  • @pvg thanks for helping me find an answer. As you point out the answer may be explained somewhere else but it was not clear to me. – juanald_reagan Aug 26 '17 at 13:57
  • pleasure. I think the best answer is really 'don't do this' , but that's somewhat out of scope of your question as stated. – pvg Aug 26 '17 at 13:59
  • @pvg - I am interested in better ways of achieving similar results - when you say don't so this...is that directed towards the linux command itself or the pythonic use of subprocess.call? – juanald_reagan Aug 26 '17 at 14:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152899/discussion-between-user1670178-and-pvg). – juanald_reagan Aug 26 '17 at 14:10

1 Answers1

2

You are executing the wrong command when you do this:

subprocess.call(["tail", "-f", "*files.log", "|", "grep", "10.10.10.100"])

You need to pass the option shell=True when using shell features like the pipeline (|):

subprocess.call("tail -f *files.log | grep 10.10.10.100", shell=True)
John Zwinck
  • 207,363
  • 31
  • 261
  • 371