简体   繁体   English

尾巴-f | 格列普 <regex> | php脚本.php <grep result>

[英]Tail -f | Grep <regex> | php script.php <grep result>

Ok, so I have a ssh connection open to a remote server. 好的,所以我已经打开了到远程服务器的ssh连接。 I'm running a tail on the logs and if an ID shows up in the logs I need to do an insert into the database. 我在日志上显示一条尾巴,如果日志中显示ID,则需要插入数据库。

So I have my ssh tail working and I have it piping into my grep function which is giving me the IDs I need. 因此,我的ssh尾部工作正常,并将其传递到我的grep函数中,这为我提供了所需的ID。 The next step is that as those IDs are found it needs to immediately kick off a php script. 下一步是找到这些ID后,需要立即启动php脚本。

What I thought it would look like is: 我认为它看起来像是:

ssh -t <user>@<host> "tail -f /APP/logs/foo.log" | grep -oh "'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'" | php myscript.php <grep result>

And yes my regex is horrible, wanted to use [0-9]{8}, but what I have gets the job done. 是的,我的正则表达式太可怕了,想使用[0-9] {8},但是我已经完成了工作。

Any suggestions? 有什么建议么? Tried looking at -exec, awk, or any other tool. 尝试查看-exec,awk或任何其他工具。 I could write the result to its own file and then read the new file, but that doesn't catch the streaming ids. 我可以将结果写入其自己的文件中,然后读取新文件,但这不能捕获流ID。

-=-=-=-=-EDIT-=-=-=-=-=- -=-=-=-=-编辑-=-=-=-=-=-

So here is what I'm using: 所以这是我正在使用的:

ssh -t <user>@<host> "tail -f /APP/logs/foo.log" |grep "^javax.ejb.ObjectNotFoundException" |awk '/[0-9]/ { system("php myscript.php "$6) }'

And if I use tail -l #lines it works, or if after a while I ctrl-c, it then works. 而且,如果我使用tail -l #lines可以正常工作,或者如果过一会儿ctrl-c则可以正常工作。 The behavior I wanted though was to as the tail got a bad ID to kick off the script to fix the bad ID. 我想要的行为是,因为尾部有一个错误的ID以启动脚本来修复错误的ID。 Not wait until an EOF or some tail buffer... 不要等到EOF或一些尾部缓冲区...

I'm having similar problem. 我有类似的问题。 There's something funny with tail -f and grep -o combination when ssh. ssh时,tail -f和grep -o组合会有些有趣。

So on local server, if you do 所以在本地服务器上,如果您这样做

tail -f myfile.log |grep -o keyword

It grep just fine. 它grep就好了。

But if you do it from remote server.... 但是,如果您从远程服务器上执行此操作。

ssh user@server 'tail -f myfile.log |grep -o keyword'

doesn't work 不起作用

But if you remove -f from tail or -o from grep, work just fine... weird :-/ 但是,如果您从尾部删除-f或从grep中删除-o,则工作得很好...很奇怪:-/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM