优化frpc保活
This commit is contained in:
@@ -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<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
|
||||
List<String> 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(
|
||||
|
||||
Reference in New Issue
Block a user