From 93b222da99dd4c2a21a0908c3db64ed21263a38b Mon Sep 17 00:00:00 2001 From: gaofei Date: Mon, 13 Jan 2025 17:30:38 +0800 Subject: [PATCH] Fix kill on linux --- Cargo.toml | 2 +- README.md | 2 +- src/commands/stop.rs | 30 ++++++++++++++++++++---------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8546926..b8907e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pmr" -version = "0.1.0" +version = "0.0.1" edition = "2021" authors = ["Your Name "] description = "A process manager in Rust" diff --git a/README.md b/README.md index 68c593e..bc1b9df 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,5 @@ pmr list/ls pmr stop [id;name] pmr stop [id;name] pmr restart [id;name] -pmr delete/rm [idname] +pmr delete/rm [id:name] ``` \ No newline at end of file diff --git a/src/commands/stop.rs b/src/commands/stop.rs index eba9627..ebed108 100644 --- a/src/commands/stop.rs +++ b/src/commands/stop.rs @@ -12,11 +12,16 @@ pub fn stop_process(target: &str, show_list: bool) { if let Ok(pmr_id) = target.parse::() { if let Some(process) = processes.iter().find(|p| p.pmr_id == pmr_id) { if process.pid > 0 { - // 在Windows上使用taskkill命令终止进程 - let output = Command::new("taskkill") - .args(&["/PID", &process.pid.to_string(), "/F"]) - .output() - .expect("无法执行taskkill命令"); + // 根据操作系统使用不同的命令终止进程 + let output = if cfg!(target_os = "windows") { + Command::new("taskkill") + .args(&["/PID", &process.pid.to_string(), "/F"]) + .output() + } else { + Command::new("kill") + .args(&["-9", &process.pid.to_string()]) + .output() + }.expect("无法执行进程终止命令"); if output.status.success() { println!("已停止进程 '{}' (PID: {})", process.name, process.pid); @@ -43,11 +48,16 @@ pub fn stop_process(target: &str, show_list: bool) { if !found { if let Some(process) = processes.iter().find(|p| p.name == target) { if process.pid > 0 { - // 在Windows上使用taskkill命令终止进程 - let output = Command::new("taskkill") - .args(&["/PID", &process.pid.to_string(), "/F"]) - .output() - .expect("无法执行taskkill命令"); + // 根据操作系统使用不同的命令终止进程 + let output = if cfg!(target_os = "windows") { + Command::new("taskkill") + .args(&["/PID", &process.pid.to_string(), "/F"]) + .output() + } else { + Command::new("kill") + .args(&["-9", &process.pid.to_string()]) + .output() + }.expect("无法执行进程终止命令"); if output.status.success() { println!("已停止进程 '{}' (PID: {})", process.name, process.pid);