diff --git a/src/commands/restart.rs b/src/commands/restart.rs index 97aaea8..49aeb9f 100644 --- a/src/commands/restart.rs +++ b/src/commands/restart.rs @@ -33,7 +33,7 @@ pub fn restart_process(config: Option, target: Option, args: Ve fn restart_existing_process(process: &PmrProcessInfo) { println!("正在重启进程 '{}'...", process.name); - + // 先停止进程 stop_process(&process.pmr_id.to_string(), false); diff --git a/src/commands/start.rs b/src/commands/start.rs index 39e902b..9a38965 100644 --- a/src/commands/start.rs +++ b/src/commands/start.rs @@ -14,7 +14,12 @@ struct Config { args: Vec, } -pub fn start_process(config: Option, name: Option, target: Option, args: Vec) { +pub fn start_process( + config: Option, + name: Option, + target: Option, + args: Vec, +) { let dump_config = DumpConfig::get_instance(); // 如果指定了target,先检查是否是已存在的进程 @@ -42,8 +47,10 @@ pub fn start_process(config: Option, name: Option, target: Opti if let Some(ref config_path) = config { let mut file = File::open(config_path).expect("Failed to open config file"); let mut contents = String::new(); - file.read_to_string(&mut contents).expect("Failed to read config file"); - let config: Config = serde_json::from_str(&contents).expect("Failed to parse config file"); + file.read_to_string(&mut contents) + .expect("Failed to read config file"); + let config: Config = + serde_json::from_str(&contents).expect("Failed to parse config file"); config.name } else { "unnamed".to_string() diff --git a/src/commands/stop.rs b/src/commands/stop.rs index ebed108..fd1b279 100644 --- a/src/commands/stop.rs +++ b/src/commands/stop.rs @@ -12,6 +12,32 @@ 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 { + // 检查进程是否真实在运行 + 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") { Command::new("taskkill") @@ -21,7 +47,10 @@ pub fn stop_process(target: &str, show_list: bool) { Command::new("kill") .args(&["-9", &process.pid.to_string()]) .output() - }.expect("无法执行进程终止命令"); + } + .expect("无法执行进程终止命令"); + + println!("--- test output: {}", output.status); if output.status.success() { println!("已停止进程 '{}' (PID: {})", process.name, process.pid); @@ -39,6 +68,9 @@ pub fn stop_process(target: &str, show_list: bool) { } } else { println!("进程 '{}' 已经停止", process.name); + dump_config + .update_process_status(process.pmr_id, 0, "stopped".to_string()) + .expect("无法更新进程状态"); found = true; } } @@ -48,6 +80,32 @@ 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 { + // 检查进程是否真实在运行 + 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") { Command::new("taskkill") @@ -57,7 +115,8 @@ pub fn stop_process(target: &str, show_list: bool) { Command::new("kill") .args(&["-9", &process.pid.to_string()]) .output() - }.expect("无法执行进程终止命令"); + } + .expect("无法执行进程终止命令"); if output.status.success() { println!("已停止进程 '{}' (PID: {})", process.name, process.pid); @@ -74,6 +133,9 @@ pub fn stop_process(target: &str, show_list: bool) { } } else { println!("进程 '{}' 已经停止", process.name); + dump_config + .update_process_status(process.pmr_id, 0, "stopped".to_string()) + .expect("无法更新进程状态"); } } else { eprintln!("未找到进程: {}", target);