2

I have a Process started like that:

Process process = new ProcessBuilder("sh", "-c", "mongod | tee /tmp/logfile.txt");

This process, when it's interrupted, gracefully ends, writing to the output stream "dbexit: really exiting now"

So later, I want to kill it:

InputStream inputStream = process.getInputStream()
Scanner scanner = new Scanner(inputStream).useDelimiter(Pattern.quote("dbexit: really exiting now"));
//pid taken from grep
Shell.runShellCommand("kill -2 " + pid).waitFor();
scanner.next()

This always runs successfully on my machine. But almost never on CI server. scanner.next() throws:

java.util.NoSuchElementException: null
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)

So it seems like the process ended (or at least closed it's InputStream) before writing "dbexit: really exiting now". But this text is present at the end of /tmp/logfile.txt. Is it possible that process InputStream was closed, while it was still writing? And that it was written to the file?

4

1 回答 1

1

输入流被缓冲。您从缓冲区读取数据,这些数据是在进程被终止之前写入的。

于 2013-04-15T17:24:01.343 回答