From ccc43fe70e98e680c116b8840c7d2fb551f1c3ce Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Sun, 7 Dec 2025 18:13:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EWindows=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.sh | 1 + backend/start-monitor.sh | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 backend/build.sh diff --git a/backend/build.sh b/backend/build.sh new file mode 100644 index 0000000..7f2ae6b --- /dev/null +++ b/backend/build.sh @@ -0,0 +1 @@ +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o monitor-server main.go diff --git a/backend/start-monitor.sh b/backend/start-monitor.sh index 789d664..649662e 100755 --- a/backend/start-monitor.sh +++ b/backend/start-monitor.sh @@ -3,13 +3,15 @@ # ===================== 配置区 ===================== # 程序路径 -AGENT_PATH="/root/monitor/monitor-agent" +AGENT_PATH="/path/to/monitor-server" # 日志文件路径 -LOG_FILE="/root/monitor/agent.log" +LOG_FILE="/path/to/server.log" # PID文件路径(记录进程ID) -PID_FILE="/root/monitor/agent.pid" +PID_FILE="/pat/to/server.pid" # 启动参数(根据实际需求调整) START_ARGS="" +# 工作目录 +WORK_DIR="" # ==================== 配置区结束 ==================== # 检查程序文件是否存在 @@ -48,18 +50,35 @@ start_agent() { return 0 fi - echo "🚀 正在启动 monitor-agent..." - # 创建日志目录(如果不存在) + echo "🚀 正在启动 monitor-agent(工作目录:${WORK_DIR})..." + + # 新增:检查并切换工作目录 + if [ ! -d "${WORK_DIR}" ]; then + echo "⚠️ 工作目录 ${WORK_DIR} 不存在,正在创建..." + mkdir -p "${WORK_DIR}" + if [ $? -ne 0 ]; then + echo "❌ 创建工作目录 ${WORK_DIR} 失败!" + exit 1 + fi + fi + + # 切换到工作目录(关键:程序将在此目录下运行) + cd "${WORK_DIR}" || { + echo "❌ 切换到工作目录 ${WORK_DIR} 失败!" + exit 1 + } + + # 创建日志目录 mkdir -p "$(dirname ${LOG_FILE})" - # 后台启动程序,重定向日志,记录PID + # 后台启动程序(注意:cd仅影响当前子进程,需在同一行执行) nohup "${AGENT_PATH}" ${START_ARGS} > "${LOG_FILE}" 2>&1 & AGENT_PID=$! echo "${AGENT_PID}" > "${PID_FILE}" - # 等待2秒检查是否启动成功 + # 等待检查启动状态 sleep 2 if check_running; then - echo "✅ monitor-agent 启动成功(PID: ${AGENT_PID})" + echo "✅ monitor-agent 启动成功(PID: ${AGENT_PID},工作目录:${WORK_DIR})" echo "日志文件:${LOG_FILE}" else echo "❌ monitor-agent 启动失败!请查看日志:${LOG_FILE}"