0

I have a process builder that initiates a process that "tails" new line added to a really big log file.

Right now I'm

ProcessBuilder m_builder = new ProcessBuilder(fullCmdList);
m_builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
m_builder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process m = m_builder.start();
m.waitFor();

redirecting output to stdout. Instead I'd like to detect when the process in question writes anything (I don't care what it writes) and trigger certain events.

Is there a way I can detect when a process writes it's first bit/byte to it's output stream when invoked via process builder?

Srini
  • 1,619
  • 1
  • 19
  • 34
  • 2
    Use [`Process#getInputStream`](http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getInputStream()) and read from it, which is the process's out, which is demonstrated within [this answer](http://stackoverflow.com/questions/25177372/how-to-get-the-output-of-a-java-program/25177554#25177554) – MadProgrammer Mar 25 '15 at 01:30
  • I don't want to read the entire length of the stream. I'd like to cut and run the moment the first character is written – Srini Mar 25 '15 at 01:36
  • Then don't use a while-loop `InputStream#read` is a blocking call, it will wait until there is something to read. The danger is, some process's will hang if you don't read there output... – MadProgrammer Mar 25 '15 at 01:37
  • Using `Process.getInputStream()` doesn't commit you to reading to the end of it, but starting any process that produces output *does* commit you to that. You can trigger your actions when the first data arrives, but you still have to read to end of stream: otherwise the process is liable to block. – user207421 Mar 25 '15 at 02:13

0 Answers0