简体   繁体   English

远程服务器中的processbuilder监视器日志文件

[英]processbuilder monitor log file in remote server

I am trying to: 我在尝试着:

  1. Execute Bash script 执行Bash脚本
  2. Read log file 读取日志文件

I have following code using processBuilder which has readLog function that reads log file and executeBash which executes the command and then destroys processbuilder's readlog process. 我有以下使用processBuilder的代码,该程序具有readLog函数(可读取日志文件)和executeBash(可执行命令,然后销毁processbuilder的readlog进程)。 But as readLog file is reading a live log file executeBash never gets called. 但是由于readLog文件正在读取实时日志文件,所以executeBash永远不会被调用。

How do I read the log file in real time and display it simultaneously with 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);
    }
}

According to javadoc, 根据javadoc,

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. 如有必要,使当前线程等待,直到此Process对象表示的进程终止。 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. 如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出。

So the readProcess.waitFor(); 因此, readProcess.waitFor(); will block the main thread and you can never expect executeBash(system); 将阻塞主线程,您将永远无法期待executeBash(system); to be called before readProcess has terminated. 在readProcess终止之前被调用。

You can start a thread to launch 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM