• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ZeroTurnaround Process Killer

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.10</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:

  • check whether a process is alive
  • wait until a process has finished
  • terminate a process gracefully (by default disabled on Windows as it's unsupported - WindowsProcess)
  • terminate a process forcibly
  • get the process ID (PID) of running JVM

and these non-functional requirements:

  • abstraction of processes regardless they are started from Java or not (use process ID) - SystemProcess
  • have process API similar to java.lang.Process
  • all waiting methods should have one version with timeout and another without it
  • stopping operation should be idempotent - if a process was already finished it is a success
  • separate generic behavior from process implementation - ProcessUtil
  • support alternative implementations for stopping same process - OrProcess
  • simple factory for creating process instances - Processes
  • invoke Java 8 java.lang.Process methods if possible - Java8Process

Limitations:

  • As the process abstraction is also used for already running processes it can't access their streams or exit value.
  • The scope of this project is stopping single processes. However WindowsProcess has method setIncludeChildren to also stop child processes in case the root process is still running.

Examples

  • Get my PID
int pid = PidUtil.getMyPid();
System.out.println("My PID is " + pid);

  • Check whether a PID is alive
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);

  • Check whether a process is alive
Process p = new ProcessBuilder("my-application").start();
JavaProcess process = Processes.newJavaProcess(p);
boolean isAlive = process.isAlive();
System.out.println("Process " + process + " is alive: " + isAlive);

  • Wait until an already started process has finished
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);

  • Wait until the started process has finished
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);

  • Stop an already started process, timeout of 30 seconds for graceful stop, timeout of 10 seconds for forceful stop
int pid = Integer.parseInt(FileUtils.readFileToString(new File("pidfile")));
PidProcess process = Processes.newPidProcess(pid);
ProcessUtil.destroyGracefullyOrForcefullyAndWait(process, 30, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);

  • Stop the started process, timeout of 30 seconds for graceful stop, timeout of 10 seconds for forceful stop
Process p = new ProcessBuilder("my-application").start();
SystemProcess process = Processes.newStandardProcess(p);
ProcessUtil.destroyGracefullyOrForcefullyAndWait(process, 30, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);

More Repositories

1

zt-zip

ZeroTurnaround ZIP Library
Java
1,373
star
2

sql-formatter

A whitespace formatter for different query languages
TypeScript
1,114
star
3

zt-exec

ZeroTurnaround Process Executor
Java
876
star
4

jvm-languages-report

Combined repository for the JVM languages report by RebelLabs
Java
63
star
5

callspy

A simple tracing agent
Java
63
star
6

gradle-jrebel-plugin

The plugin generates rebel.xml configuration file for the Gradle-based project
Java
51
star
7

maven-jrebel-plugin

Generates rebel.xml configuration file for the maven project
Java
51
star
8

stardate-converter

Converts common date to Star Trek stardate
JavaScript
20
star
9

xrebel-docker-demo

Demo project for showcasing XRebel features with distributed apps
Shell
9
star
10

jenkins-reporter

Generate nice reports of your Jenkins views.
Java
7
star
11

giantrobots

7
star
12

isJRebel

Check if a String is equal to JRebel
Java
7
star
13

bad-classes

Utility to debug UnsupportedClassVersionError in Java
Java
6
star
14

zt-hock

JavaScript
4
star
15

strict-spies

A strict alternative for Jasmine spies
JavaScript
4
star
16

atg-hello-demo

Simple demo application for Oracle ATG Web Commerce
Java
4
star
17

build-flow-test-aggregator

Java
4
star
18

jenkins-liverebel-plugin

Jenkins LiveRebel Plugin
Java
3
star
19

cluster-stats

Jenkins Cluster Statistics Plugin
Java
2
star
20

jrebel-tdd

Java
2
star
21

xrebel-demo-supplements

Application included into https://github.com/zeroturnaround/xrebel-docker-demo
Java
2
star
22

eslint-config-zt

ESLint shareable config for ZeroTurnaround JavaScript projects
JavaScript
1
star
23

xr-demo-answers

JavaScript
1
star
24

lunch-bot

JavaScript
1
star
25

rebellabs-report-2017-analysis-app

An analysis app for the data received for the RebelLabs Developer Productivity Report 2017
Java
1
star
26

zt-react-components

Commonly used React components for ZeroTurnaround products
JavaScript
1
star
27

aws-ecs-deploy-jenkins-plugin

Java
1
star
28

netbeans-jrebel-open-plugin

JRebel installer plugin for NetBeans
Java
1
star