Fix: stop dead process

This commit is contained in:
gaofei 2025-01-14 11:01:59 +08:00
parent 93b222da99
commit 363c5fd681
3 changed files with 75 additions and 6 deletions

View File

@ -14,7 +14,12 @@ struct Config {
args: Vec<String>, args: Vec<String>,
} }
pub fn start_process(config: Option<PathBuf>, name: Option<String>, target: Option<String>, args: Vec<String>) { pub fn start_process(
config: Option<PathBuf>,
name: Option<String>,
target: Option<String>,
args: Vec<String>,
) {
let dump_config = DumpConfig::get_instance(); let dump_config = DumpConfig::get_instance();
// 如果指定了target先检查是否是已存在的进程 // 如果指定了target先检查是否是已存在的进程
@ -42,8 +47,10 @@ pub fn start_process(config: Option<PathBuf>, name: Option<String>, target: Opti
if let Some(ref config_path) = config { if let Some(ref config_path) = config {
let mut file = File::open(config_path).expect("Failed to open config file"); let mut file = File::open(config_path).expect("Failed to open config file");
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).expect("Failed to read config file"); file.read_to_string(&mut contents)
let config: Config = serde_json::from_str(&contents).expect("Failed to parse config file"); .expect("Failed to read config file");
let config: Config =
serde_json::from_str(&contents).expect("Failed to parse config file");
config.name config.name
} else { } else {
"unnamed".to_string() "unnamed".to_string()

View File

@ -12,6 +12,32 @@ 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 {
// 检查进程是否真实在运行
let is_running = if cfg!(target_os = "windows") {
Command::new("tasklist")
.args(&["/FI", &format!("PID eq {}", process.pid)])
.output()
.map(|output| {
String::from_utf8_lossy(&output.stdout)
.contains(&process.pid.to_string())
})
.unwrap_or(false)
} else {
Command::new("ps")
.args(&["-p", &process.pid.to_string()])
.output()
.map(|output| output.status.success())
.unwrap_or(false)
};
if !is_running {
println!("进程 '{}' (PID: {}) 未在运行", process.name, process.pid);
dump_config
.update_process_status(process.pmr_id, 0, "stopped".to_string())
.expect("无法更新进程状态");
found = true;
}
// 根据操作系统使用不同的命令终止进程 // 根据操作系统使用不同的命令终止进程
let output = if cfg!(target_os = "windows") { let output = if cfg!(target_os = "windows") {
Command::new("taskkill") Command::new("taskkill")
@ -21,7 +47,10 @@ pub fn stop_process(target: &str, show_list: bool) {
Command::new("kill") Command::new("kill")
.args(&["-9", &process.pid.to_string()]) .args(&["-9", &process.pid.to_string()])
.output() .output()
}.expect("无法执行进程终止命令"); }
.expect("无法执行进程终止命令");
println!("--- test output: {}", output.status);
if output.status.success() { if output.status.success() {
println!("已停止进程 '{}' (PID: {})", process.name, process.pid); println!("已停止进程 '{}' (PID: {})", process.name, process.pid);
@ -39,6 +68,9 @@ pub fn stop_process(target: &str, show_list: bool) {
} }
} else { } else {
println!("进程 '{}' 已经停止", process.name); println!("进程 '{}' 已经停止", process.name);
dump_config
.update_process_status(process.pmr_id, 0, "stopped".to_string())
.expect("无法更新进程状态");
found = true; found = true;
} }
} }
@ -48,6 +80,32 @@ 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 {
// 检查进程是否真实在运行
let is_running = if cfg!(target_os = "windows") {
Command::new("tasklist")
.args(&["/FI", &format!("PID eq {}", process.pid)])
.output()
.map(|output| {
String::from_utf8_lossy(&output.stdout)
.contains(&process.pid.to_string())
})
.unwrap_or(false)
} else {
Command::new("ps")
.args(&["-p", &process.pid.to_string()])
.output()
.map(|output| output.status.success())
.unwrap_or(false)
};
if !is_running {
println!("进程 '{}' (PID: {}) 未在运行", process.name, process.pid);
dump_config
.update_process_status(process.pmr_id, 0, "stopped".to_string())
.expect("无法更新进程状态");
return;
}
// 根据操作系统使用不同的命令终止进程 // 根据操作系统使用不同的命令终止进程
let output = if cfg!(target_os = "windows") { let output = if cfg!(target_os = "windows") {
Command::new("taskkill") Command::new("taskkill")
@ -57,7 +115,8 @@ pub fn stop_process(target: &str, show_list: bool) {
Command::new("kill") Command::new("kill")
.args(&["-9", &process.pid.to_string()]) .args(&["-9", &process.pid.to_string()])
.output() .output()
}.expect("无法执行进程终止命令"); }
.expect("无法执行进程终止命令");
if output.status.success() { if output.status.success() {
println!("已停止进程 '{}' (PID: {})", process.name, process.pid); println!("已停止进程 '{}' (PID: {})", process.name, process.pid);
@ -74,6 +133,9 @@ pub fn stop_process(target: &str, show_list: bool) {
} }
} else { } else {
println!("进程 '{}' 已经停止", process.name); println!("进程 '{}' 已经停止", process.name);
dump_config
.update_process_status(process.pmr_id, 0, "stopped".to_string())
.expect("无法更新进程状态");
} }
} else { } else {
eprintln!("未找到进程: {}", target); eprintln!("未找到进程: {}", target);