Home

Awesome

ZT Process Killer

Continuous Integration

Build Status

Quick Overview

The project was created in ZeroTurnaround to have a stable base functionality of stopping running processes from Java. It can stop processes started from Java (e.g. with zt-exec) as well as existing system processes based on their process ID (PID).

Installation

Maven Central

The project artifacts are available in Maven Central Repository.

To include it in your maven project then you have to specify the dependency.

...
<dependency>
    <groupId>org.zeroturnaround</groupId>
    <artifactId>zt-process-killer</artifactId>
    <version>1.11</version>
</dependency>
...

Motivation

In Java Process.destroy() is ambiguous. On Windows it terminates processes forcibly. On UNIX it terminates them gracefully. As invoking kill commands from Java is errorprone it should have an advised API and good test coverage.

We had the following functional requirements:

and these non-functional requirements:

Limitations:

Examples

int pid = PidUtil.getMyPid();
System.out.println("My PID is " + pid);
<hr/>
int pid = Integer.parseInt(FileUtils.readFileToString(new File("pidfile")));
PidProcess process = Processes.newPidProcess(pid);
boolean isAlive = process.isAlive();
System.out.println("PID " + pid + " is alive: " + isAlive);
<hr/>
Process p = new ProcessBuilder("my-application").start();
JavaProcess process = Processes.newJavaProcess(p);
boolean isAlive = process.isAlive();
System.out.println("Process " + process + " is alive: " + isAlive);
<hr/>
int pid = Integer.parseInt(FileUtils.readFileToString(new File("pidfile")));
PidProcess process = Processes.newPidProcess(pid);
boolean finished = process.waitFor(10, TimeUnit.MINUTES);
System.out.println("PID " + pid + " finished on time: " + finished);
<hr/>
Process p = new ProcessBuilder("my-application").start();
JavaProcess process = Processes.newJavaProcess(p);
boolean finished = process.waitFor(10, TimeUnit.MINUTES);
System.out.println("Process " + process + " finished on time: " + finished);
<hr/>
int pid = Integer.parseInt(FileUtils.readFileToString(new File("pidfile")));
PidProcess process = Processes.newPidProcess(pid);
ProcessUtil.destroyGracefullyOrForcefullyAndWait(process, 30, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
<hr/>
Process p = new ProcessBuilder("my-application").start();
SystemProcess process = Processes.newStandardProcess(p);
ProcessUtil.destroyGracefullyOrForcefullyAndWait(process, 30, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);