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