pkill_lib/
lib.rs

1mod process;
2
3pub use anyhow::Result;
4pub use process::ProcessQuery;
5use sysinfo::{ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt};
6
7/// Initialize [`System`] instance with only process information loaded
8fn init_system() -> System {
9    let refresh_kind = RefreshKind::new().with_processes(ProcessRefreshKind::everything());
10    let mut sys = System::new_with_specifics(refresh_kind);
11    sys.refresh_processes();
12    sys
13}
14
15/// Iterate `targets` to find and kill any processes that are found
16pub fn pkill(targets: Vec<ProcessQuery>) -> Result<()> {
17    let sys = init_system();
18
19    let processes = targets
20        .iter()
21        .flat_map(|query| process::search(&sys, &query));
22
23    for process in processes {
24        println!("killing process {}", process.pid());
25        if !process.kill() {
26            eprintln!("kill signal failed to send")
27        }
28    }
29
30    Ok(())
31}