Skip to content

Commit a27ce96

Browse files
cli kill background process and all its sub-processes
1 parent 8738062 commit a27ce96

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/ProcessBackgroundRunResult.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
package org.testingisdocumenting.webtau.cli;
1818

19+
import java.io.IOException;
20+
import java.lang.reflect.Field;
21+
1922
public class ProcessBackgroundRunResult {
2023
private final Process process;
2124
private final StreamGobbler outputGobbler;
2225
private final StreamGobbler errorGobbler;
2326

27+
private final int pid;
28+
2429
private final Thread consumeErrorThread;
2530
private final Thread consumeOutThread;
2631

@@ -30,6 +35,7 @@ public ProcessBackgroundRunResult(Process process,
3035
Thread consumeErrorThread,
3136
Thread consumeOutThread) {
3237
this.process = process;
38+
this.pid = extractPid(process);
3339
this.outputGobbler = outputGobbler;
3440
this.errorGobbler = errorGobbler;
3541
this.consumeErrorThread = consumeErrorThread;
@@ -43,7 +49,13 @@ public Process getProcess() {
4349
public void destroy() {
4450
outputGobbler.close();
4551
errorGobbler.close();
46-
process.destroy();
52+
53+
try {
54+
ProcessUtils.kill(pid);
55+
process.waitFor();
56+
} catch (InterruptedException | IOException e) {
57+
throw new RuntimeException(e);
58+
}
4759
}
4860

4961
public StreamGobbler getOutputGobbler() {
@@ -69,4 +81,20 @@ public ProcessRunResult createRunResult() {
6981
outputGobbler.getException(),
7082
errorGobbler.getException());
7183
}
84+
85+
/**
86+
* we need to support java 8, pid() is added in java 9
87+
* so using hacks to get to pid value
88+
* @param process process
89+
* @return pid
90+
*/
91+
private static int extractPid(Process process) {
92+
try {
93+
Field pidField = process.getClass().getDeclaredField("pid");
94+
pidField.setAccessible(true);
95+
return (int) pidField.get(process);
96+
} catch (NoSuchFieldException | IllegalAccessException e) {
97+
throw new RuntimeException(e);
98+
}
99+
}
72100
}

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/ProcessUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.nio.file.Paths;
2121
import java.util.Arrays;
22+
import java.util.Collections;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.stream.Collectors;
@@ -46,6 +47,10 @@ public static ProcessRunResult run(String command, Map<String, String> env) thro
4647
}
4748
}
4849

50+
public static void kill(int pid) throws IOException {
51+
run("pkill -TERM -P " + pid, Collections.emptyMap());
52+
}
53+
4954
public static ProcessBackgroundRunResult runInBackground(String command, Map<String, String> env) throws IOException {
5055
List<String> splitCommandWithPrefix = prefixCommandWithPathAndSplit(command);
5156
ProcessBuilder processBuilder = new ProcessBuilder(splitCommandWithPrefix);

0 commit comments

Comments
 (0)