python 服务日志监控 tail -f

elixu 2013-11-16 03:41:41
import subprocess
#path & name of log
filename = '/home/eli/codebase/refactor/api-server/log/online-config-development.log'
#shell command to be executed
command = 'tail -f '+filename+' | grep '+'groupcast'
popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print(command)
while True:
#get the content which the grep command get
print(popen.stdout.readline().strip())


执行之后就是打印不出来结果, 但是我直接在shell中输入打印的command就有结果。
python高手帮忙看下问题。
...全文
336 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
那闯 2013-11-20
  • 打赏
  • 举报
回复
1楼说的是shell的方式吧! subprocess.Popen 会创建子进程, 也许子进程还没执行完,你就开始读当然读不到, 调用wait()等,子进程完成再read。
linux-c 2013-11-19
  • 打赏
  • 举报
回复
cmd=`tail -f filename |grep keyword`
elixu 2013-11-19
  • 打赏
  • 举报
回复
引用 3 楼 Mr_Ringht 的回复:
[quote=引用 2 楼 elixu 的回复:] [quote=引用 1 楼 Mr_Ringht 的回复:] cmd=`shell cmd`-------这里是反引号 1旁边那个键 print cmd
你的意思是 command = 'tail -f '+filename+' | grep '+'groupcast' 改成 command = `tail -f` +filename+` | grep `+`groupcast` 是这样的吗[/quote] 是的,你试试可以不[/quote] 不行,报错File "moniter.py", line 6 command = `tail -f` +filename+` | grep `+`groupcast` ^ SyntaxError: invalid syntax
linux-c 2013-11-19
  • 打赏
  • 举报
回复
引用 2 楼 elixu 的回复:
[quote=引用 1 楼 Mr_Ringht 的回复:] cmd=`shell cmd`-------这里是反引号 1旁边那个键 print cmd
你的意思是 command = 'tail -f '+filename+' | grep '+'groupcast' 改成 command = `tail -f` +filename+` | grep `+`groupcast` 是这样的吗[/quote] 是的,你试试可以不
elixu 2013-11-18
  • 打赏
  • 举报
回复
引用 1 楼 Mr_Ringht 的回复:
cmd=`shell cmd`-------这里是反引号 1旁边那个键 print cmd
你的意思是 command = 'tail -f '+filename+' | grep '+'groupcast' 改成 command = `tail -f` +filename+` | grep `+`groupcast` 是这样的吗
linux-c 2013-11-17
  • 打赏
  • 举报
回复
cmd=`shell cmd`-------这里是反引号 1旁边那个键 print cmd

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧