Package com.glitchybyte.glib.process
Class GProcessTask
java.lang.Object
com.glitchybyte.glib.concurrent.GTask
com.glitchybyte.glib.process.GProcessTask
- All Implemented Interfaces:
Runnable
An encapsulation that represents and manages a system process.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionGProcessTask(String[] command) Convenience constructor to quickly execute a process without any concern for its output.GProcessTask(String[] command, Path dir) Convenience constructor to quickly execute a process without any concern for its output.GProcessTask(String[] command, Path dir, boolean autoPrintOutput) Creates a process from the given command and starting directory.GProcessTask(String[] command, Path dir, int maxOutputBufferLines) Creates a process from the given command and starting directory. -
Method Summary
Modifier and TypeMethodDescriptionvoidawaitForState(GProcessTask.State state) Wait indefinitely until a given state is reached.booleanawaitForState(GProcessTask.State state, Duration timeout) Wait until a given state is reached or timeout expires.Returns all collected output until this moment.Returns the process handle to query for other information about the process.getState()Gets the process state.Returns the status code of the finished process, or null if the process was interrupted.voidrun()voidstop()Stops the process.voidstop(long pid) Stops this process or any subprocess of this process.Methods inherited from class com.glitchybyte.glib.concurrent.GTask
awaitDone, getTaskRunner, getTaskThreadName, interrupt, isDone, setTaskRunner, started
-
Constructor Details
-
GProcessTask
Creates a process from the given command and starting directory.- Parameters:
command- Array representing the parts of the command.dir- Starting directory. If null, it will inherit the current process current directory.maxOutputBufferLines- Max lines to keep in memory from console output of this process.
-
GProcessTask
Creates a process from the given command and starting directory.- Parameters:
command- Array representing the parts of the command.dir- Starting directory. If null, it will inherit the current process current directory.autoPrintOutput- This convenience parameter makes it so output of the process gets continually printed to the current process' console.
-
GProcessTask
Convenience constructor to quickly execute a process without any concern for its output.- Parameters:
command- Array representing the parts of the command.dir- Starting directory. If null, it will inherit the current process current directory.
-
GProcessTask
Convenience constructor to quickly execute a process without any concern for its output. It will also default to the current process' current directory.- Parameters:
command- Array representing the parts of the command.
-
-
Method Details
-
getProcessHandle
Returns the process handle to query for other information about the process.- Returns:
- The process handle to query for other information about the process.
-
run
public void run() -
getStatusCode
Returns the status code of the finished process, or null if the process was interrupted.- Returns:
- The status code of the finished process, or null if the process was interrupted.
-
stop
public void stop(long pid) Stops this process or any subprocess of this process.The implementation will send a SIGINT to the given pid.
*IMPORTANT!* This is a convenience for when you need to inform a subprocess you want to exit for orderly shutdown up the chain. For example, an application started by a Windows batch script will not shut down if you send a SIGINT to the batch process.
- Parameters:
pid- Pid of the descendant process to stop.
-
stop
public void stop()Stops the process.The implementation will send a SIGINT.
-
getState
Gets the process state.- Returns:
- The process state.
-
awaitForState
public boolean awaitForState(GProcessTask.State state, Duration timeout) throws InterruptedException Wait until a given state is reached or timeout expires.- Parameters:
state- Wanted state.timeout- Time to wait for state.- Returns:
- True if the state has been reached.
- Throws:
InterruptedException- If the wait was interrupted.
-
awaitForState
Wait indefinitely until a given state is reached.- Parameters:
state- Wanted state.- Throws:
InterruptedException- If the wait was interrupted.
-
getOutput
Returns all collected output until this moment. Resets collection buffer.- Returns:
- All collected lines until this moment.
-