gpt4 book ai didi

java - processbuilder 监控远程服务器中的日志文件

转载 作者:行者123 更新时间:2023-12-01 09:33:08 32 4
gpt4 key购买 nike

我正在尝试:

  1. 执行 Bash 脚本
  2. 读取日志文件

我有以下使用 processBuilder 的代码,它具有读取日志文件的 readLog 函数和执行命令的executeBash,然后销毁 processbuilder 的 readlog 进程。但由于 readLog 文件正在读取实时日志文件,executeBash 永远不会被调用。

如何实时读取日志文件并与executeBash同时显示?

<小时/>
public void performAction(OssSystem system) {
readLog(system);
executeBash(system);
}
<小时/>
public void executeBash(OssSystem system) {
try {
Process process = new ProcessBuilder().command("/bin/sh", system.getCommand_path(), system.getParam()).start();

BufferedReader reader
= new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
int status = process.waitFor();
cmdResult = builder.toString() + " command status " + status;
int exitValue = process.exitValue();
readProcess.destroy();
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
LOG.log(Level.SEVERE, null, ex);
}

}
<小时/>
String logOutput;

Process readProcess;

public void readLog(OssSystem system) {

String user = system.getUsername() + "@" + system.getIp();
String cmd = "tail -f " + system.getLog_dir() + " | less";
try {
readProcess = new ProcessBuilder().command("/usr/bin/ssh", user, cmd).start();
int status = readProcess.waitFor();
BufferedReader reader
= new BufferedReader(new InputStreamReader(readProcess.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
logOutput = builder.toString() + " command status " + status;
} catch (IOException ex) {
Logger.getLogger(Shell.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(Shell.class.getName()).log(Level.SEVERE, null, ex);
}
}

最佳答案

根据javadoc,

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

所以readProcess.waitFor();会阻塞主线程,你永远不会想到 executeBash(system);在 readProcess 终止之前调用。

您可以启动一个线程来启动 readProcess .

public void performAction(OssSystem system) {
readLog(system);
ExecuteBashThread thread = new ExecuteBashThread(system);
thread.start();
}

class ExecuteBashThread extends Thread{
private OssSystem system;
public ExecuteBashThread(OssSystem system){
this.system = system;
}
@Override
public void run(){
executeBash(system);
}
}

关于java - processbuilder 监控远程服务器中的日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231378/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com