1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use tokio::process::Child;
use tokio::{
process::Command,
};
pub async fn kill_process(child: &mut Child) {
if let Some(pid) = child.id() {
#[cfg(unix)]
{
let output = Command::new("ps")
.arg("-o")
.arg("pid=")
.arg("--ppid")
.arg(pid.to_string())
.output()
.await;
match output {
Ok(output) => {
let pids = String::from_utf8_lossy(&output.stdout)
.lines()
.filter_map(|l| l.trim().parse::<u32>().ok())
.collect::<Vec<_>>();
for child_pid in pids {
let _ = Command::new("kill")
.arg("-9")
.arg(child_pid.to_string())
.status()
.await;
}
let _ = Command::new("kill")
.arg("-9")
.arg(pid.to_string())
.status()
.await;
}
Err(_) => {
let _ = Command::new("kill")
.arg("-9")
.arg(pid.to_string())
.status()
.await;
}
}
}
#[cfg(windows)]
{
let _ = Command::new("taskkill")
.arg("/PID")
.arg(pid.to_string())
.arg("/T")
.arg("/F")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.await;
}
}
}