Java library for watching (and killing) processes.
Watch Dog thread runs only when there are same active processes to watch.
.-------------. . . * *
/_/_/_/_/_/_/_/ \ * . ) .
//_/_/_/_/_/_// _ \ __ . .
/_/_/_/_/_/_/_/|/ \.' .`-o
| ||-'(/ ,--'
| || _ |
| ||'' ||
|_____________|| |_|L hjm
- Java 6
Copy the Maven dependency into your Maven project:
<dependency>
<groupId>cz.net21.ttulka.exec</groupId>
<artifactId>process-watch-dog</artifactId>
<version>1.1.0</version>
</dependency>
ProcessWatchDog watchDog = new ProcessWatchDog();
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
watchDog.watch(p, 1000); // kill the process after 1 sec
pb = new ProcessBuilder("otherCommand");
Process p2 = pb.start();
watchDog.watch(p2, 2000); // kill the second process after 2 secs
watchDog.unwatch(p);
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.
watchDog.heartBeat(p);
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:
WatchedProcess wp = watchDog.watch(p, 1000); // use the returned watched process object
wp.heartBeat();
Watched process with a heartbeat.
WatchedProcess
class added.heartBeat(Process)
method added to theProcessWatchDog
class.
Initial version.