Class ProcessUtil

java.lang.Object
org.zeroturnaround.process.ProcessUtil

public class ProcessUtil extends Object
Additional helper methods for killing processes and waiting until they finish.

Here all methods that use a timeout throw TimeoutException including the given timeout in the message instead of returning a false like SystemProcess does. Also all methods log a message in case the operation succeeded including the time it took.

Notice that methods that destroy a process do not include destroying operation itself in the timeout period. They start measuring time after sending the destroy signal. Also if the current thread is interrupted it may also interrupt sending the destroy signal itself not just the waiting period after it. So if the current thread gets interrupted there's no guarantee that the target process actually got signaled.

See Also:
  • Constructor Details

    • ProcessUtil

      public ProcessUtil()
  • Method Details

    • waitFor

      public static void waitFor(SystemProcess process) throws InterruptedException
      Waits until the given process finishes or the current thread is interrupted.
      Parameters:
      process - the target process.
      Throws:
      InterruptedException - if the current thread was interrupted.
    • waitFor

      public static void waitFor(SystemProcess process, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
      Waits until the given process finishes, a timeout is reached or the current thread is interrupted.
      Parameters:
      process - the target process.
      timeout - the maximum time to wait until the process finishes.
      unit - the time unit of the timeout argument.
      Throws:
      InterruptedException - if the current thread was interrupted.
      TimeoutException - if timeout was reached before the process finished.
    • destroyGracefullyAndWait

      public static void destroyGracefullyAndWait(SystemProcess process) throws IOException, InterruptedException
      Destroys the given process gracefully and waits until it finishes or the current thread is interrupted.
      Parameters:
      process - the target process.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
    • destroyGracefullyAndWait

      public static void destroyGracefullyAndWait(SystemProcess process, long timeout, TimeUnit unit) throws IOException, InterruptedException, TimeoutException
      Destroys the given process gracefully and waits until it finishes, a timeout occurs or the current thread is interrupted.
      Parameters:
      process - the target process.
      timeout - the maximum time to wait until the process finishes.
      unit - the time unit of the timeout argument.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
      TimeoutException - if timeout was reached before the process finished.
    • destroyForcefullyAndWait

      public static void destroyForcefullyAndWait(SystemProcess process) throws IOException, InterruptedException
      Destroys the given process forcefully and waits until it finishes or the current thread is interrupted.
      Parameters:
      process - the target process.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
    • destroyForcefullyAndWait

      public static void destroyForcefullyAndWait(SystemProcess process, long timeout, TimeUnit unit) throws IOException, InterruptedException, TimeoutException
      Destroys the given process forcefully and waits until it finishes, a timeout occurs or the current thread is interrupted.
      Parameters:
      process - the target process.
      timeout - the maximum time to wait until the process finishes.
      unit - the time unit of the timeout argument.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
      TimeoutException - if timeout was reached before the process finished.
    • destroyGracefullyOrForcefullyAndWait

      public static void destroyGracefullyOrForcefullyAndWait(SystemProcess process) throws IOException, InterruptedException
      Destroys the given process gracefully and waits until it finishes or the current thread is interrupted. If the graceful destroy operation throws an exception (e.g. it's unsupported) it destroys the process forcefully and waits until it finishes or the current thread is interrupted.
      Parameters:
      process - the target process.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
    • destroyGracefullyOrForcefullyAndWait

      public static void destroyGracefullyOrForcefullyAndWait(SystemProcess process, long gracefulTimeout, TimeUnit gracefulTimeoutUnit) throws IOException, InterruptedException
      Destroys the given process gracefully and waits until it finishes, a timeout occurs or the current thread is interrupted. If the graceful destroy operation throws an exception (e.g. it's unsupported) or a timeout is reached it destroys the process forcefully and waits until it finishes or the current thread is interrupted (no timeout is used in this case).
      Parameters:
      process - the target process.
      gracefulTimeout - the maximum time to wait until the process finishes after the graceful destroy operation.
      gracefulTimeoutUnit - the time unit of the timeout argument.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
    • destroyGracefullyOrForcefullyAndWait

      public static void destroyGracefullyOrForcefullyAndWait(SystemProcess process, long gracefulTimeout, TimeUnit gracefulTimeoutUnit, long forcefulTimeout, TimeUnit forcefulTimeoutUnit) throws IOException, InterruptedException, TimeoutException
      Destroys the given process gracefully and waits until it finishes, first timeout occurs or the current thread is interrupted. If the graceful destroy operation throws an exception (e.g. it's unsupported) or a timeout is reached it destroys the process forcefully and waits until it finishes, second timeout occurs or the current thread is interrupted.
      Parameters:
      process - the target process.
      gracefulTimeout - the maximum time to wait until the process finishes after the graceful destroy operation.
      gracefulTimeoutUnit - the time unit of the gracefulTimeout argument.
      forcefulTimeout - the maximum time to wait until the process finishes after the forceful destroy operation.
      forcefulTimeoutUnit - the time unit of the forcefulTimeout argument.
      Throws:
      IOException - on IO error.
      InterruptedException - if the current thread was interrupted.
      TimeoutException - if timeout was reached before the process finished (after the forceful destroy operation).