Class ProcessUtils
java.lang.Object
com.sun.enterprise.universal.process.ProcessUtils
Includes a somewhat kludgy way to get the pid for "me". Another casualty of
the JDK catering to the LEAST common denominator. Some obscure OS might not
have a pid! The name returned from the JMX method is like so: 12345
- Author:
- bnevins, David Matejcek
-
Method Summary
Modifier and TypeMethodDescriptionstatic FileLook for name in the Path.static booleanisAlive(long pid) static booleanstatic booleanisAlive(ProcessHandle process) TheProcess.isAlive()returns true even for zombies so we implemented this method which considers zombies as dead.static booleanisListening(HostAndPort endpoint) static voidKill the process with the given Process ID and wait until it's gone - that means that the watchedPidFile is deleted OR the process is not resolved as alive by theProcessHandle.isAlive()OR we cannot retrieve the command line of the process viaProcessHandle.Info.commandLine().static Longstatic voidsaveCurrentPid(File pidFile) Saves current pid file to the file.static booleanstatic booleanwaitWhileIsAlive(long pid, Duration timeout, boolean printDots) Blocks until the pid's handle exits or timeout comes first.static booleanwaitWhileListening(HostAndPort endpoint, Duration timeout, boolean printDots) Blocks until the endpoint closes the connection or timeout comes first.
-
Method Details
-
waitWhileIsAlive
Blocks until the pid's handle exits or timeout comes first.- Parameters:
pid- process identifiertimeout-printDots- true to print dots to STDOUT while waiting. One dot per second.- Returns:
- true if the handle was not found or exited before timeout. False otherwise.
-
isAlive
- Parameters:
pidFile-- Returns:
- true if the pid file exists and the process with the pid inside is alive.
-
isAlive
public static boolean isAlive(long pid) - Parameters:
pid-- Returns:
- true if the process with is alive.
-
isAlive
TheProcess.isAlive()returns true even for zombies so we implemented this method which considers zombies as dead.- Parameters:
process-- Returns:
- true if the process with is alive.
-
waitWhileListening
Blocks until the endpoint closes the connection or timeout comes first.- Parameters:
endpoint- endpoint host and port to use.timeout-printDots- true to print dots to STDOUT while waiting. One dot per second.- Returns:
- true if the connection was closed before timeout. False otherwise.
-
isListening
- Parameters:
endpoint- endpoint host and port to use.- Returns:
- true if the endpoint is listening on socket
-
kill
public static void kill(File pidFile, Duration timeout, boolean printDots) throws KillNotPossibleException, KillTimeoutException Kill the process with the given Process ID and wait until it's gone - that means that the watchedPidFile is deleted OR the process is not resolved as alive by theProcessHandle.isAlive()OR we cannot retrieve the command line of the process viaProcessHandle.Info.commandLine().- Parameters:
pidFile- - used to load pidtimeout- - timeout to wait until to meet conditions meaning that the process stoppedprintDots- - print one dot per second when waiting.- Throws:
KillNotPossibleException- It wasn't possible to send the kill signal to the process.KillTimeoutException- Signal was sent, but process is still alive after the timeout.
-
waitFor
- Parameters:
sign- logic defining what we are waiting for.timeout-printDots- print dot each second and new line in the end.- Returns:
- true if the sign returned true before timeout.
-
getExe
Look for name in the Path. If it is found and if it is executable then return a File object pointing to it. Otherwise return nu- Parameters:
name- the name of the file with no path- Returns:
- the File object or null
-
saveCurrentPid
Saves current pid file to the file.- Parameters:
pidFile-- Throws:
IOException
-
loadPid
- Parameters:
pidFile- file containing pid.- Returns:
- pid from the file or null if the file does not exist or is null
- Throws:
IllegalArgumentException- for unparseable file
-