优化frpc保活

This commit is contained in:
gaoyutao
2025-12-24 19:57:09 +08:00
parent 6dbee99194
commit cebcf56adf
@@ -908,32 +908,24 @@ public class AgentServiceImpl implements AgentService {
String resMsg = "frpc.toml修改成功"; String resMsg = "frpc.toml修改成功";
try { try {
// 停止 // 停止可能残留的进程
ProcessBuilder killPb = new ProcessBuilder( new ProcessBuilder("pkill", "-9", "-f", "frpc.*toml").start().waitFor();
"bash", "-c",
"pkill -f 'frpc.*toml'"
);
killPb.start().waitFor();
Thread.sleep(1000); Thread.sleep(1000);
// 启动 - 使用完整的shell命令 String frpcPath = properties.getFrpPath() + "/frpc";
String startCommand = String.format(
"nohup %s/frpc -c %s/frpc.toml > %s/frpc.log 2>&1 &",
properties.getFrpPath(),
properties.getFrpPath(),
properties.getFrpPath()
);
ProcessBuilder startPb = new ProcessBuilder( File frpcFile = new File(frpcPath);
"bash", "-c", if (!frpcFile.exists() || !frpcFile.canExecute()) {
startCommand AssertLog.error("frpc可执行文件不存在或无执行权限: {}", frpcPath);
); resCode = 0;
startPb.start(); resMsg = "frpc可执行文件不存在或无执行权限:" + frpcPath;
}else{
// 等待一小段时间确保进程启动 String scriptPath = properties.getFrpPath() + "/frpc_start.sh";
Thread.sleep(2000);
// 执行启动脚本
Process process = new ProcessBuilder("bash", scriptPath).start();
int exitCode = process.waitFor();
}
} catch (Exception e) { } catch (Exception e) {
resCode = 0; resCode = 0;
resMsg = "操作失败: " + e.getMessage(); resMsg = "操作失败: " + e.getMessage();
@@ -2030,29 +2022,45 @@ public class AgentServiceImpl implements AgentService {
} }
public boolean startFrpc() { public boolean startFrpc() {
try { try {
// 停止可能残留的进程 // 停止可能残留的进程
new ProcessBuilder("pkill", "-f", "frpc.*toml").start().waitFor(); new ProcessBuilder("pkill", "-9", "-f", "frpc.*toml").start().waitFor();
Thread.sleep(1000);
// 构建启动命令 String frpPath = properties.getFrpPath();
String frpcPath = properties.getFrpPath() + "/frpc"; String scriptPath = frpPath + "/frpc_start.sh";
String configPath = properties.getFrpPath() + "/frpc.toml";
File frpcFile = new File(frpcPath); // 检查脚本是否存在
if (!frpcFile.exists() || !frpcFile.canExecute()) { File scriptFile = new File(scriptPath);
AssertLog.error("frpc可执行文件不存在或无执行权限: {}", frpcPath); if (!scriptFile.exists()) {
AssertLog.error("启动脚本不存在: {}", scriptPath);
return false; return false;
} }
StringBuilder startCommandBuilder = new StringBuilder();
startCommandBuilder.append("nohup ").append(frpcPath).append(" -c ").append(configPath);
ProcessBuilder pb = new ProcessBuilder( // 检查脚本权限
"bash", "-c", if (!scriptFile.canExecute()) {
startCommandBuilder.toString() new ProcessBuilder("chmod", "+x", scriptPath).start().waitFor();
}
// 关键修改:不等待脚本完成
String asyncCommand = String.format(
"bash -c '%s > %s/startup.log 2>&1 &'",
scriptPath, frpPath
); );
pb.start();
// 等待2秒后检查是否启动成功 ProcessBuilder pb = new ProcessBuilder("bash", "-c", asyncCommand);
Thread.sleep(2000); Process process = pb.start();
// 不等待脚本进程
new Thread(() -> {
try {
process.waitFor();
} catch (Exception e) {
// 忽略
}
}).start();
// 等待3秒后检查
Thread.sleep(3000);
return isFrpcRunning(); return isFrpcRunning();
} catch (Exception e) { } catch (Exception e) {