Is it possible to get the output of the tail -f command to a java program?

In accordance with:

Java IO implementation of unix / linux "tail -f"

Java "tail -f" wrapper

I assume this is 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 } 

The process p ends. It works great with teams that do not have constant updates unlike "top" or "tail -f".

Interestingly, I missed something, are there some limitations in the buffer, or something else? I use eclipse, but I think that this should not affect the behavior of the process.

Actually, maybe there is another easy way to solve my problem that I missed. I use tail -f because I have to analyze a rather bold (some GB) log that is constantly being added and is not going to open it and read the whole file. I only need to control the add lines. However, I did not find a suitable tail implementation

Thanks in advance.

+4
source share
1 answer

You cannot execute commands with channels from inside java.

If you want to receive messages on channels, run the command '/ bin / sh' as ​​follows:

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

In addition, your Java program should consume the output of the process quickly enough, so its buffers do not overflow.

You can read this article when Runtime.exec () will not , to describe common errors when trying to execute a process from Java.

+2
source

All Articles