Skip to content

Commit e9bb545

Browse files
committed
examples: add cleanup_children
1 parent ae6f3be commit e9bb545

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

crates/examples/readme/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition.workspace = true
66
[dependencies]
77
kill_tree = { path = "../../libs/kill_tree", features = ["tokio"] }
88
tokio = { version = "1.36.0", features = ["full"] }
9+
ctrlc = "3.4.2"
910

1011
[[bin]]
1112
name = "kill_tree"
@@ -18,3 +19,7 @@ path = "src/kill_tree_sigkill.rs"
1819
[[bin]]
1920
name = "kill_tree_tokio"
2021
path = "src/kill_tree_tokio.rs"
22+
23+
[[bin]]
24+
name = "cleanup_children"
25+
path = "src/cleanup_children.rs"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use kill_tree::{blocking::kill_tree_with_config, Config};
2+
use std::sync::atomic::{AtomicBool, Ordering};
3+
use std::sync::Arc;
4+
5+
fn cleanup_children() {
6+
let current_process_id = std::process::id();
7+
let config = Config {
8+
include_target: false,
9+
..Default::default()
10+
};
11+
let result = kill_tree_with_config(current_process_id, &config);
12+
println!("kill_tree_with_config: {:?}", result);
13+
}
14+
15+
fn main() {
16+
let running = Arc::new(AtomicBool::new(true));
17+
let r = running.clone();
18+
19+
ctrlc::set_handler(move || {
20+
cleanup_children();
21+
r.store(false, Ordering::SeqCst);
22+
})
23+
.expect("Error setting Ctrl-C handler");
24+
25+
println!("Waiting for Ctrl-C...");
26+
while running.load(Ordering::SeqCst) {}
27+
println!("Got it! Exiting...");
28+
}

0 commit comments

Comments
 (0)