Fix: add workdir for process

This commit is contained in:
gaofei 2025-01-14 11:20:47 +08:00
parent 363c5fd681
commit 4186d220ce
3 changed files with 26 additions and 2 deletions

View File

@ -8,5 +8,6 @@ pub struct PmrProcessInfo {
pub namespace: String, pub namespace: String,
pub status: String, pub status: String,
pub program: String, pub program: String,
pub workdir: String,
pub args: Vec<String>, pub args: Vec<String>,
} }

View File

@ -2,6 +2,7 @@ use super::super::base::process::PmrProcessInfo;
use super::super::config::dump::DumpConfig; use super::super::config::dump::DumpConfig;
use super::list::list_processes; use super::list::list_processes;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
@ -21,6 +22,11 @@ pub fn start_process(
args: Vec<String>, args: Vec<String>,
) { ) {
let dump_config = DumpConfig::get_instance(); let dump_config = DumpConfig::get_instance();
// 获取当前工作目录
let workdir = env::current_dir()
.unwrap_or_else(|_| PathBuf::from("."))
.to_string_lossy()
.into_owned();
// 如果指定了target先检查是否是已存在的进程 // 如果指定了target先检查是否是已存在的进程
if let Some(ref target_str) = target { if let Some(ref target_str) = target {
@ -89,6 +95,7 @@ pub fn start_process(
.add_process( .add_process(
process_name, process_name,
"default".to_string(), "default".to_string(),
workdir,
config.program, config.program,
pid, pid,
"running".to_string(), "running".to_string(),
@ -116,6 +123,7 @@ pub fn start_process(
.add_process( .add_process(
process_name, process_name,
"default".to_string(), "default".to_string(),
workdir,
target_program, target_program,
pid, pid,
"running".to_string(), "running".to_string(),
@ -138,6 +146,15 @@ fn start_existing_process(process: &PmrProcessInfo) {
return; return;
} }
// 保存当前工作目录
let original_dir = env::current_dir().expect("无法获取当前工作目录");
// 切换到进程的工作目录
if let Err(e) = env::set_current_dir(&process.workdir) {
eprintln!("无法切换到工作目录 {}: {}", process.workdir, e);
return;
}
let mut cmd = Command::new(&process.program); let mut cmd = Command::new(&process.program);
cmd.args(&process.args) cmd.args(&process.args)
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
@ -154,11 +171,15 @@ fn start_existing_process(process: &PmrProcessInfo) {
.expect("无法更新进程状态"); .expect("无法更新进程状态");
// 显示进程列表 // 显示进程列表
println!("\n当前进程列表:");
list_processes(false); list_processes(false);
} }
Err(e) => { Err(e) => {
eprintln!("启动进程 '{}' 失败: {}", process.name, e); eprintln!("启动进程失败: {}", e);
} }
} }
// 恢复原始工作目录
if let Err(e) = env::set_current_dir(&original_dir) {
eprintln!("警告:无法恢复原始工作目录: {}", e);
}
} }

View File

@ -57,6 +57,7 @@ impl DumpConfig {
&self, &self,
name: String, name: String,
namespace: String, namespace: String,
workdir: String,
program: String, program: String,
pid: u32, pid: u32,
status: String, status: String,
@ -71,6 +72,7 @@ impl DumpConfig {
namespace, namespace,
pid, pid,
status, status,
workdir,
program, program,
args, args,
}); });