https://github.com/ttulka/process-watch-dog
Process Watch Dog Java Library
https://github.com/ttulka/process-watch-dog
heartbeat java java-library process process-monitor process-watch-dog processes watch
Last synced: about 1 month ago
JSON representation
Process Watch Dog Java Library
- Host: GitHub
- URL: https://github.com/ttulka/process-watch-dog
- Owner: ttulka
- License: apache-2.0
- Created: 2017-10-07T14:39:03.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-20T19:26:26.000Z (almost 8 years ago)
- Last Synced: 2025-07-27T03:09:30.415Z (11 months ago)
- Topics: heartbeat, java, java-library, process, process-monitor, process-watch-dog, processes, watch
- Language: Java
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Process Watch Dog
**Java library for watching (and *killing*) processes.**
Watch Dog thread runs only when there are same active processes to watch.
```
.-------------. . . * *
/_/_/_/_/_/_/_/ \ * . ) .
//_/_/_/_/_/_// _ \ __ . .
/_/_/_/_/_/_/_/|/ \.' .`-o
| ||-'(/ ,--'
| || _ |
| ||'' ||
|_____________|| |_|L hjm
```
## Prerequisites
- Java 6
## Usage
Copy the Maven dependency into your Maven project:
```xml
cz.net21.ttulka.exec
process-watch-dog
1.1.0
```
### Watch a Process
#### Create a Watch Dog object:
```java
ProcessWatchDog watchDog = new ProcessWatchDog();
```
#### Create a process and watch it by the Watch Dog:
```java
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
watchDog.watch(p, 1000); // kill the process after 1 sec
```
#### Watch another process by the same Watch Dog:
```java
pb = new ProcessBuilder("otherCommand");
Process p2 = pb.start();
watchDog.watch(p2, 2000); // kill the second process after 2 secs
```
### Unwatch a Process
#### Unwatch a process if you don't care any longer:
```java
watchDog.unwatch(p);
```
### Keep a Process Alive
Normally, a process should be killed only after a timeout of inactivity.
To tell the Watch Dog that the process is still active a heartbeat must be sent.
#### Send a heartbeat explicitly to reset the timeout:
```java
watchDog.heartBeat(p);
```
#### Send a heartbeat automatically with every read byte:
```java
p = watchDog.watch(p, 1000); // reassign the `WatchedProcess` object to the process reference
InputStream is = p.getInputStream();
int b;
while ((b = is.read()) != -1) {
// heartbeat is sent implicitly with every successful call of `read()`
}
```
Alternatively, send a heartbeat explicitly via `WatchedProcess` object:
```java
WatchedProcess wp = watchDog.watch(p, 1000); // use the returned watched process object
wp.heartBeat();
```
## Release Changes
### 1.1.0
Watched process with a heartbeat.
- `WatchedProcess` class added.
- `heartBeat(Process)` method added to the `ProcessWatchDog` class.
### 1.0.0
Initial version.
## License
[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)