Package com.peterphi.std.system.exec
Class BasicProcessTracker
java.lang.Object
com.peterphi.std.system.exec.BasicProcessTracker
A basic process tracker that does not touch stdout/stderr streams
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
BasicProcessTracker
(List<String> cmd, Process p, boolean combinedOutput) -
Method Summary
Modifier and TypeMethodDescriptionprotected Thread
copy
(InputStream in, Writer out) protected Thread
discard
(InputStream in) void
Indicates that the output of this process should be discardedint
exitCode()
Returns the exit code of the application, assuming it has already terminated.protected void
finished
(int exitCode) boolean
Determines if the application has completed yetprotected boolean
Returns true if the output of this process is still being readfinal void
kill()
void
kill
(boolean force) protected void
int
Wait an indefinite amount of time for the process to exitint
waitForExit
(int expected) Wait an indefinite amount of time for the process to exit, expecting the return code to beexpected
.int
waitForExit
(Deadline deadline) Waits for the process to exit; this method blocks until the process has completed (at which point it returns the process exit code) or untildeadline
has elapsed, at which point it returns Integer.MIN_VALUEint
waitForExit
(Deadline deadline, int expected) Wait untildeadline
for the process to exit, expecting the return code to beexpected
.
-
Field Details
-
cmd
-
process
-
combinedOutput
protected final boolean combinedOutput -
finished
protected boolean finished -
exitCode
protected int exitCode
-
-
Constructor Details
-
BasicProcessTracker
-
-
Method Details
-
isStillReadingOutput
protected boolean isStillReadingOutput()Returns true if the output of this process is still being read- Returns:
- false if this instance is copying the stdout/stderr and they have been closed, otherwise true if we are still copying stdout/stderr. Always returns false if some external agent is handling the stdout/stderr streams.
-
discardOutput
public void discardOutput()Indicates that the output of this process should be discarded -
getProcess
-
kill
public final void kill() -
kill
public void kill(boolean force) -
isFinished
public boolean isFinished()Determines if the application has completed yet- Returns:
- true if the process has terminated, otherwise false
-
exitCode
Returns the exit code of the application, assuming it has already terminated. If the process has not yet terminated then an IllegalStateException will be thrown- Returns:
- the exit code of the process
- Throws:
IllegalStateException
- if the process has not yet terminatedIllegalThreadStateException
-
waitForExit
public int waitForExit(int expected) Wait an indefinite amount of time for the process to exit, expecting the return code to beexpected
. If the output is notexpected
then a RuntimeException is thrown- Parameters:
expected
- the expected return code- Returns:
- the exit code of the process
- Throws:
RuntimeException
- if the return code was not what was expected
-
waitForExit
Wait untildeadline
for the process to exit, expecting the return code to beexpected
. If the output is notexpected
(or if the operation times out) then a RuntimeException is thrown
In the event of a timeout the process is not terminated- Parameters:
deadline
-expected
-- Returns:
- the exit code of the process
- Throws:
RuntimeException
- if a timeout occurrs or if the return code was not what was expected
-
waitForExit
public int waitForExit()Wait an indefinite amount of time for the process to exit- Returns:
- the exit code of the process
-
waitForExit
Waits for the process to exit; this method blocks until the process has completed (at which point it returns the process exit code) or untildeadline
has elapsed, at which point it returns Integer.MIN_VALUE- Parameters:
deadline
-- Returns:
- the exit code of the process
-
getStandardOutputStream
-
getStandardErrorStream
-
getStandardInputStream
-
finished
protected void finished(int exitCode) -
unexpectedFailure
-
copy
-
discard
-