从Java

时间:2016-08-02 11:40:21

标签: java unix

由于我有太多的apache日志文件,我决定创建一个java程序构建命令让我运行apachetop。所以我决定创建一个索引文件,其中将放置所有apache_log文件。

apache_logs_index

/opt/lampp/logs/access_log
/opt/lampp/logs/error_log
/opt/lampp/logs/php_error_log
/opt/lampp/logs/ssl_request_log
/opt/lampp/logs/mb.domain.com-access_log
/opt/lampp/logs/mb.domain.com-error_log
/opt/lampp/logs/my.domain.com-access_log
/opt/lampp/logs/my.domain.com-error_log
/opt/lampp/logs/op.domain.com-access_log
/opt/lampp/logs/op.domain.com-error_log

我创建了一个java程序来为我构建命令。

ApacheTopCall.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ApacheTopCall{

        public static void main(String args[]) {

                String fileName = "apache_logs_index";
                String command = "apachetop";
                command = command + " -d 1";
                try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

                        String line;
                        while ((line = br.readLine()) != null) {
                                command = command + " -f " + line.trim();
                        }
                        //System.out.println(command);
                        ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", command);
                        Process p = pb.start();
                        p.waitFor();
                } catch (IOException|InterruptedException e) {
                        e.printStackTrace();
                }

        }

}

现在我想要的只是自己运行apachetop。我尝试使用ProcessBuilder调用它,但apachetop没有显示,而只是等待进程退出。当我运行我的java代码时,我希望执行命令并运行apachetop

当我执行java代码构建的命令时,它运行良好。但不是ProcessBuilder

1 个答案:

答案 0 :(得分:1)

如果你想实现的只是这个,那么这是一个简单而有效的解决方案:

您还需要对Java代码进行一些小的更改,如下所示:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ApacheTopCall{

        public static void main(String args[]) {

                String fileName = "apache_logs_index";
                String command = "apachetop";
                command = command + " -d 1 -q -l -r -p"; //extra params
                try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

                        String line;
                        while ((line = br.readLine()) != null) {
                                command = command + " -f " + line.trim();
                        }
                        System.out.println(command); //we are just creating the command for run. The C program actually run the generated command.
                        //ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", command);
                        //Process p = pb.start();
                        //p.waitFor();
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

}

我创建了一个简单的C程序apachetopall.c

#include <stdio.h>

int main(){
        FILE *fp = popen("cd /root/scripts/; java ApacheTopCall", "r"); //make sure you cd to the location of your java class.
        char cmd[32768];
        fgets(cmd, 32768, fp);
        system(cmd);
        return 0;
}

使用以下命令编译并将其保存为apachetopall

gcc -o apachetopall apachetopall.c

现在您需要做的就是执行二进制文件apachetopall。你的目标已经实现。

工作原理

我们要求java创建需要触发的命令,而不是让java运行命令。现在,C程序运行java代码并获取命令作为输出,它将触发相同的命令,该命令将转到屏幕的前面。