use std::io;
use std::process::Command as CommandSys;
pub fn kill_by_pid(pid: i64) -> Result<(), KillByPidError> {
let mut cmd = build_kill_command(true, std::iter::once(pid), None);
let output = cmd.output().map_err(KillByPidError::Output)?;
match output.status.success() {
true => Ok(()),
false => Err(KillByPidError::KillProcess),
}
}
pub enum KillByPidError {
Output(io::Error),
KillProcess,
}
pub fn build_kill_command(
force: bool,
pids: impl Iterator<Item = i64>,
signal: Option<u32>,
) -> CommandSys {
if cfg!(windows) {
let mut cmd = CommandSys::new("taskkill");
if force {
cmd.arg("/F");
}
for id in pids {
cmd.arg("/PID");
cmd.arg(id.to_string());
}
cmd
} else {
let mut cmd = CommandSys::new("kill");
if let Some(signal_value) = signal {
cmd.arg(format!("-{signal_value}"));
} else if force {
cmd.arg("-9");
}
cmd.args(pids.map(move |id| id.to_string()));
cmd
}
}