Package org.kiwiproject.base.process
Class ProcessHelper
java.lang.Object
org.kiwiproject.base.process.ProcessHelper
Wrapper class around the static utility functions in
Processes that requires an instance, adn thus by using
an instance of this class instead of Processes directly, it will make it much easier to test code that deals
with processes.
Note that most of the methods are intended only for use on Unix/Linux operating systems.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindChildProcessId(long parentProcessId) For a given parent process id, find one and only one child process.findChildProcessIds(long parentProcessId) For a given parent process id, find any child processes.intkill(long processId, String signal, long timeout, TimeUnit unit, KillTimeoutAction action) Kill a process, waiting up totimeoutin the specifiedTimeUnitfor it to terminate.intkill(long processId, String signal, KillTimeoutAction action) Kill a process, waiting up toProcesses.DEFAULT_KILL_TIMEOUT_SECONDSseconds for it to terminate.intkill(long processId, KillSignal signal, long timeout, TimeUnit unit, KillTimeoutAction action) Kill a process, waiting up totimeoutin the specifiedTimeUnitfor it to terminate.intkill(long processId, KillSignal signal, KillTimeoutAction action) Kill a process, waiting up toProcesses.DEFAULT_KILL_TIMEOUT_SECONDSseconds for it to terminate.booleankillForcibly(Process process, long timeout, TimeUnit unit) Equivalent to akill -9(i.e. aSIGKILL).Launches a new process using the specifiedworkingDirectoryandcommand.Launches a new process using the specifiedcommand.Launches a new process using the specifiedcommand.Does apgrepwith the specified full command.Does apgrepwith the specified full command.Does apgrepwith the specified full command.Does apgrepwith the specified full command.pgrepParsedList(String commandLine) Does apgrepfor the specified full command, returning a list of pairs containing the process id (pid) and the matched command line.pgrepParsedList(String user, String commandLine) Does apgrepfor the specified full command, returning a list of pairs containing the process id (pid) and the matched command line.pgrepWithSingleResult(String commandLine) Does apgrepagainst the specified full command, expecting a single result, or no result.pgrepWithSingleResult(String user, String commandLine) Does apgrepagainst the specified full command, expecting a single result for a specific user, or no result.waitForExit(Process process) Waits up toProcesses.DEFAULT_WAIT_FOR_EXIT_TIME_SECONDSfor the given process to exit.waitForExit(Process process, long timeout, TimeUnit unit) Waits up to the specifiedtimeoutfor the given process to exit.Locate a program in the user's path.whichAsPath(String program) Locate a program in the user's path, returning the result as aPath.
-
Constructor Details
-
ProcessHelper
public ProcessHelper()
-
-
Method Details
-
waitForExit
Waits up toProcesses.DEFAULT_WAIT_FOR_EXIT_TIME_SECONDSfor the given process to exit.- Parameters:
process- the process to wait for- Returns:
- an
Optionalthat will contain the exit code if the process exited before the timeout, or empty if the process did not exit before the timeout expired. - See Also:
-
waitForExit
Waits up to the specifiedtimeoutfor the given process to exit.- Parameters:
process- the process to wait fortimeout- the value of the time to waitunit- the unit of time to wait- Returns:
- an
Optionalthat will contain the exit code if the process exited before the timeout, or empty if the process did not exit before the timeout expired. - See Also:
-
launch
Launches a new process using the specifiedcommand.- Parameters:
command- the list containing the program and its arguments- Returns:
- the new
Process - See Also:
-
launch
Launches a new process using the specifiedworkingDirectoryandcommand.- Parameters:
workingDirectory- the working directory to usecommand- the list containing the program and its arguments- Returns:
- the new
Process - See Also:
-
launch
Launches a new process using the specifiedcommand.- Parameters:
command- a list containing the program and its arguments- Returns:
- the new
Process - See Also:
-
pgrep
Does apgrepwith the specified full command.- Parameters:
commandLine- the full command to match- Returns:
- a list of matching process ids (pids)
- See Also:
-
pgrep
Does apgrepwith the specified full command.- Parameters:
user- the OS user (passed to the-uoption)commandLine- the full command to match- Returns:
- list of matching process ids (pids)
- See Also:
-
pgrepWithSingleResult
Does apgrepagainst the specified full command, expecting a single result, or no result.- Parameters:
commandLine- the full command line- Returns:
- an optional either containing a process id, or an empty optional
- See Also:
-
pgrepWithSingleResult
Does apgrepagainst the specified full command, expecting a single result for a specific user, or no result.- Parameters:
user- the OS user (passed to the-uoption)commandLine- the full command to match- Returns:
- an optional either containing a process id, or an empty optional
- See Also:
-
pgrepList
Does apgrepwith the specified full command.- Parameters:
commandLine- the full command line to match- Returns:
- a list of pgrep output, with each line in format "{pid} {command}"
- See Also:
-
pgrepList
Does apgrepwith the specified full command.- Parameters:
user- the OS user (passed to the-uoption)commandLine- the full command line to match- Returns:
- a list of pgrep output, with each line in format "{pid} {command}"
- See Also:
-
pgrepParsedList
Does apgrepfor the specified full command, returning a list of pairs containing the process id (pid) and the matched command line.- Parameters:
commandLine- the full command line to match- Returns:
- a list of
Pairobjects; each pair contains the pid as a Long and the associated full command - See Also:
-
pgrepParsedList
public List<org.apache.commons.lang3.tuple.Pair<Long,String>> pgrepParsedList(String user, String commandLine) Does apgrepfor the specified full command, returning a list of pairs containing the process id (pid) and the matched command line.- Parameters:
user- the OS user (passed to the-uoption)commandLine- the full command line to match- Returns:
- a list of
Pairobjects; each pair contains the pid as a Long and the associated full command - See Also:
-
kill
Kill a process, waiting up toProcesses.DEFAULT_KILL_TIMEOUT_SECONDSseconds for it to terminate.- Parameters:
processId- the pid of the process to killsignal- the kill signal; this could be the signal number (e.g. "1") or name (e.g. "SIGHUP")action- theKillTimeoutActionto take if the process doesn't terminate within the allotted time- Returns:
- the exit code from the
killcommand, or-1ifactionis - See Also:
-
kill
public int kill(long processId, KillSignal signal, long timeout, TimeUnit unit, KillTimeoutAction action) Kill a process, waiting up totimeoutin the specifiedTimeUnitfor it to terminate.- Parameters:
processId- the pid of the process to killsignal- the kill signal enumtimeout- the time to wait for the process to be killedunit- the time unit associated withtimeoutaction- theKillTimeoutActionto take if the process doesn't terminate within the allotted time- Returns:
- the exit code from the
killcommand, or-1ifactionis - See Also:
-
kill
Kill a process, waiting up toProcesses.DEFAULT_KILL_TIMEOUT_SECONDSseconds for it to terminate.- Parameters:
processId- the pid of the process to killsignal- the kill signal; this could be the signal number (e.g. "1") or name (e.g. "SIGHUP")action- theKillTimeoutActionto take if the process doesn't terminate within the allotted time- Returns:
- the exit code from the
killcommand, or-1ifactionisKillTimeoutAction.NO_OPand the kill command times out - Throws:
UncheckedIOException- if an I/O error occurs killing the process- See Also:
-
kill
public int kill(long processId, String signal, long timeout, TimeUnit unit, KillTimeoutAction action) Kill a process, waiting up totimeoutin the specifiedTimeUnitfor it to terminate.- Parameters:
processId- the pid of the process to killsignal- the kill signal; this could be the signal number (e.g. "1") or name (e.g. "SIGHUP")timeout- the time to wait for the process to be killedunit- the time unit associated withtimeoutaction- theKillTimeoutActionto take if the process doesn't terminate within the allotted time- Returns:
- the exit code from the
killcommand, or-1ifactionisKillTimeoutAction.NO_OPand the kill command times out - Throws:
UncheckedIOException- if an I/O error occurs killing the process- See Also:
-
killForcibly
public boolean killForcibly(Process process, long timeout, TimeUnit unit) throws InterruptedException Equivalent to akill -9(i.e. aSIGKILL).- Parameters:
process- the process to kill forciblytimeout- the time to wait for the process to be forcibly killedunit- the time unit associated with thetimeout- Returns:
trueifprocesswas killed before the timeout period elapsed;falseotherwise- Throws:
InterruptedException- if the current thread is interrupted while waiting- See Also:
-
findChildProcessId
For a given parent process id, find one and only one child process. There may be no child processes, in which case an emptyOptionalis returned.This method considers it an error if the process has more than one child process id.
- Parameters:
parentProcessId- the parent process id- Returns:
- optional containing the child process id if there is one; otherwise an empty optional
- Throws:
IllegalStateException- if there is more than one child process found
-
findChildProcessIds
For a given parent process id, find any child processes. There may be no child processes, in which case an empty collection is returned.- Parameters:
parentProcessId- the parent process id- Returns:
- a collection of child pids, or an empty collection if there are no child processes
-
whichAsPath
Locate a program in the user's path, returning the result as aPath.- Parameters:
program- the program to locate- Returns:
- an Optional containing the full
Pathto the program, or an empty Optional if not found - See Also:
- Implementation Note:
- If there is more than program found, only the first one is returned
-
which
Locate a program in the user's path.- Parameters:
program- the program to locate- Returns:
- an Optional containing the full path to the program, or an empty Optional if not found
- See Also:
- Implementation Note:
- If there is more than program found, only the first one is returned
-