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?