优化frpc保活

This commit is contained in:
gaoyutao
2025-12-25 18:06:23 +08:00
parent cebcf56adf
commit 13968d60aa
@@ -922,9 +922,29 @@ public class AgentServiceImpl implements AgentService {
}else{ }else{
String scriptPath = properties.getFrpPath() + "/frpc_start.sh"; String scriptPath = properties.getFrpPath() + "/frpc_start.sh";
// 执行启动脚本 // 确保脚本权限为755
Process process = new ProcessBuilder("bash", scriptPath).start(); new ProcessBuilder("chmod", "755", scriptPath).start().waitFor();
int exitCode = process.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) { } catch (Exception e) {
resCode = 0; resCode = 0;
@@ -1018,21 +1038,31 @@ public class AgentServiceImpl implements AgentService {
return config; return config;
} }
/**
* 检查frpc进程是否在运行
*/
/** /**
* 检查frpc进程是否在运行 * 检查frpc进程是否在运行
*/ */
public boolean isFrpcRunning() { public boolean isFrpcRunning() {
try { try {
ProcessBuilder pb = new ProcessBuilder( // 使用pgrep检查,最简单高效
"bash", "-c", "pgrep -f 'frpc.*toml'" 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(); int exitCode = process.waitFor();
AssertLog.info("是否运行:{}",exitCode == 0); AssertLog.info("FRPC是否运行:{}", exitCode == 0);
return exitCode == 0; return exitCode == 0;
} catch (Exception e) { } catch (Exception e) {
AssertLog.error("检测FRPC运行状态失败:{}", e.getMessage());
return false; return false;
} }
} }
@@ -1054,7 +1084,7 @@ public class AgentServiceImpl implements AgentService {
// 读取现有配置文件 // 读取现有配置文件
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
List<String> newLines = new ArrayList<>(); List<String> newLines = new ArrayList<>();
long time = System.currentTimeMillis();
for (String line : lines) { for (String line : lines) {
// 替换serverAddr // 替换serverAddr
if (line.trim().startsWith("serverAddr =")) { if (line.trim().startsWith("serverAddr =")) {
@@ -1068,6 +1098,9 @@ public class AgentServiceImpl implements AgentService {
else if (remotePort != null && line.trim().startsWith("remotePort =")) { else if (remotePort != null && line.trim().startsWith("remotePort =")) {
newLines.add("remotePort = " + remotePort); newLines.add("remotePort = " + remotePort);
} }
else if (line.trim().startsWith("name =")) {
newLines.add("name = \"" + "tcp"+time + "\"");
}
// 保持其他行不变 // 保持其他行不变
else { else {
newLines.add(line); newLines.add(line);
@@ -2036,10 +2069,8 @@ public class AgentServiceImpl implements AgentService {
return false; return false;
} }
// 检查脚本权限 // 确保脚本权限为755
if (!scriptFile.canExecute()) { new ProcessBuilder("chmod", "755", scriptPath).start().waitFor();
new ProcessBuilder("chmod", "+x", scriptPath).start().waitFor();
}
// 关键修改:不等待脚本完成 // 关键修改:不等待脚本完成
String asyncCommand = String.format( String asyncCommand = String.format(