是否有可能获得“tail -f”的输出?命令到java程序? [英] Is it possible to get output of "tail -f" command to java programme?

查看:851
本文介绍了是否有可能获得“tail -f”的输出?命令到java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据:

unix / linux的Java IO实现" tail -f"

Java" tail -f"包装器

我认为它可能但我有以下问题。

I assume its possible but I have the following problem.

String TailCommand = "tail -f /path/PATH.txt| grep (...)";
Runtime r = Runtime.getRuntime();
Process p = r.exec(TailCommand);

//handle buffer

while (running) {
    // handle output
}

进程p终止。它与没有top或tail -f的常量更新的命令一起正常工作。

Process p terminates. It works fine with commands that doesnt have constant updates unlike "top" or "tail -f".

我想知道我是否错过了什么,有一些缓冲限制或者其他什么?
我正在使用eclipse,但我想这不应该对流程行为产生任何影响。

I wonder if I missed something, there is some buffer limitations or whatever? Im using eclipse but i guess that should not have any impact on process behavior.

事实上可能还有另一种简单的方法可以解决我错过的问题。我使用tail -f,因为我必须分析相当胖(一些GB)日志,这些日志不断附加并且不打算打开它并读取整个文件。我只需要控制附加行。但是我发现没有合适的尾部实现

In fact maybe there is another simply way to solve my issue that i missed. Im using "tail -f" since i have to analyze quite fat(some GBs) log thats constantly appending and dont intend to open it and read the whole file. I need only to controll the appending lines. However i found no suitable tail implementation

提前致谢。

推荐答案

您无法从java内部执行管道命令。

You can't execute piped commands from inside java.

如果您想要管道输出,请执行'/ bin / sh',如下所示:

If you want piped output, execute '/bin/sh' like this:

String tailCommand = "/bin/sh -c 'tail -f /path/PATH.txt| grep (...)'";

此外,你的java程序必须足够快地消耗进程的输出,所以它的缓冲区不会溢出。

Also, your java program must consume the output of the process fast enough, so its buffers do not overflow.

您可能想阅读这篇文章当Runtime.exec()不会时,描述尝试从Java执行进程时常见的陷阱。

You may want to read this article When Runtime.exec() won't, for description of common pitfalls when trying to execute a process from Java.

这篇关于是否有可能获得“tail -f”的输出?命令到java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆