From 13968d60aa3abbcd929d3ecbed1c83cb32342bb5 Mon Sep 17 00:00:00 2001 From: gaoyutao Date: Thu, 25 Dec 2025 18:06:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96frpc=E4=BF=9D=E6=B4=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/service/impl/AgentServiceImpl.java | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java b/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java index 9146851..797e7d2 100644 --- a/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java +++ b/src/main/java/com/tongran/agent/client/service/impl/AgentServiceImpl.java @@ -922,9 +922,29 @@ public class AgentServiceImpl implements AgentService { }else{ String scriptPath = properties.getFrpPath() + "/frpc_start.sh"; - // 执行启动脚本 - Process process = new ProcessBuilder("bash", scriptPath).start(); - int exitCode = process.waitFor(); + // 确保脚本权限为755 + new ProcessBuilder("chmod", "755", scriptPath).start().waitFor(); + + // 关键修改:不等待脚本完成 + String asyncCommand = String.format( + "bash -c '%s > %s/startup.log 2>&1 &'", + scriptPath, frpPath + ); + + ProcessBuilder pb = new ProcessBuilder("bash", "-c", asyncCommand); + Process process = pb.start(); + + // 不等待脚本进程 + new Thread(() -> { + try { + process.waitFor(); + } catch (Exception e) { + // 忽略 + } + }).start(); + + // 等待3秒后检查 + Thread.sleep(3000); } } catch (Exception e) { resCode = 0; @@ -1018,21 +1038,31 @@ public class AgentServiceImpl implements AgentService { return config; } + /** + * 检查frpc进程是否在运行 + */ /** * 检查frpc进程是否在运行 */ public boolean isFrpcRunning() { try { - ProcessBuilder pb = new ProcessBuilder( - "bash", "-c", "pgrep -f 'frpc.*toml'" - ); + // 使用pgrep检查,最简单高效 + Process process = new ProcessBuilder("sh", "-c", "ps aux | grep -v grep | grep -q 'frpc.*toml'").start(); + + // 清空输出流 + try (InputStream is = process.getInputStream()) { + byte[] buffer = new byte[1024]; + while (is.read(buffer) > 0) { + // 忽略输出 + } + } - Process process = pb.start(); int exitCode = process.waitFor(); - AssertLog.info("是否运行:{}",exitCode == 0); + AssertLog.info("FRPC是否运行:{}", exitCode == 0); return exitCode == 0; } catch (Exception e) { + AssertLog.error("检测FRPC运行状态失败:{}", e.getMessage()); return false; } } @@ -1054,7 +1084,7 @@ public class AgentServiceImpl implements AgentService { // 读取现有配置文件 List lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); List newLines = new ArrayList<>(); - + long time = System.currentTimeMillis(); for (String line : lines) { // 替换serverAddr if (line.trim().startsWith("serverAddr =")) { @@ -1068,6 +1098,9 @@ public class AgentServiceImpl implements AgentService { else if (remotePort != null && line.trim().startsWith("remotePort =")) { newLines.add("remotePort = " + remotePort); } + else if (line.trim().startsWith("name =")) { + newLines.add("name = \"" + "tcp"+time + "\""); + } // 保持其他行不变 else { newLines.add(line); @@ -2036,10 +2069,8 @@ public class AgentServiceImpl implements AgentService { return false; } - // 检查脚本权限 - if (!scriptFile.canExecute()) { - new ProcessBuilder("chmod", "+x", scriptPath).start().waitFor(); - } + // 确保脚本权限为755 + new ProcessBuilder("chmod", "755", scriptPath).start().waitFor(); // 关键修改:不等待脚本完成 String asyncCommand = String.format(