Fix kill on linux

This commit is contained in:
gaofei 2025-01-13 17:30:38 +08:00
parent 138803713a
commit 93b222da99
3 changed files with 22 additions and 12 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "pmr" name = "pmr"
version = "0.1.0" version = "0.0.1"
edition = "2021" edition = "2021"
authors = ["Your Name <you@example.com>"] authors = ["Your Name <you@example.com>"]
description = "A process manager in Rust" description = "A process manager in Rust"

View File

@ -9,5 +9,5 @@ pmr list/ls
pmr stop [id;name] pmr stop [id;name]
pmr stop [id;name] pmr stop [id;name]
pmr restart [id;name] pmr restart [id;name]
pmr delete/rm [idname] pmr delete/rm [id:name]
``` ```

View File

@ -12,11 +12,16 @@ pub fn stop_process(target: &str, show_list: bool) {
if let Ok(pmr_id) = target.parse::<u32>() { if let Ok(pmr_id) = target.parse::<u32>() {
if let Some(process) = processes.iter().find(|p| p.pmr_id == pmr_id) { if let Some(process) = processes.iter().find(|p| p.pmr_id == pmr_id) {
if process.pid > 0 { if process.pid > 0 {
// 在Windows上使用taskkill命令终止进程 // 根据操作系统使用不同的命令终止进程
let output = Command::new("taskkill") let output = if cfg!(target_os = "windows") {
Command::new("taskkill")
.args(&["/PID", &process.pid.to_string(), "/F"]) .args(&["/PID", &process.pid.to_string(), "/F"])
.output() .output()
.expect("无法执行taskkill命令"); } else {
Command::new("kill")
.args(&["-9", &process.pid.to_string()])
.output()
}.expect("无法执行进程终止命令");
if output.status.success() { if output.status.success() {
println!("已停止进程 '{}' (PID: {})", process.name, process.pid); println!("已停止进程 '{}' (PID: {})", process.name, process.pid);
@ -43,11 +48,16 @@ pub fn stop_process(target: &str, show_list: bool) {
if !found { if !found {
if let Some(process) = processes.iter().find(|p| p.name == target) { if let Some(process) = processes.iter().find(|p| p.name == target) {
if process.pid > 0 { if process.pid > 0 {
// 在Windows上使用taskkill命令终止进程 // 根据操作系统使用不同的命令终止进程
let output = Command::new("taskkill") let output = if cfg!(target_os = "windows") {
Command::new("taskkill")
.args(&["/PID", &process.pid.to_string(), "/F"]) .args(&["/PID", &process.pid.to_string(), "/F"])
.output() .output()
.expect("无法执行taskkill命令"); } else {
Command::new("kill")
.args(&["-9", &process.pid.to_string()])
.output()
}.expect("无法执行进程终止命令");
if output.status.success() { if output.status.success() {
println!("已停止进程 '{}' (PID: {})", process.name, process.pid); println!("已停止进程 '{}' (PID: {})", process.name, process.pid);