2016-06-15 77 views
-1

我的工作,我已經用java code.I've跟蹤在Linux磁盤使用的項目執行以下代碼如何從Java調用格式化的linux shell命令?

public void checkSystemPerformance() { 
    System.out.println("------- Track Disk Usage -------"); 

     BufferedReader stdInput = null; 
     BufferedReader stdError = null; 
     String line = ""; 

     try { 
      String diskUsageCommandTest = "df -h"; 
      //String diskUsageCommandTest = "df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\n\", $3,$2,$5}'";I want the result of this command via java code 

      Process proc = Runtime.getRuntime().exec(diskUsageCommandTest); 

      stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
      stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 

      // read the output from the command 
      System.out.println("Here is the standard output of the command:\n"); 
      while((line = stdInput .readLine()) != null) { 
       System.out.println(line); 
      } 

      // read any errors from the attempted command 
      System.out.println("Here is the standard error of the command (if any):\n"); 
      while ((line = stdError.readLine()) != null) { 
       System.out.println(line); 
      } 

      proc.waitFor(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println(e.getMessage()); 
     } 
    } 

在那裏我得到以下輸出:

------- Track Memory Usage,Disk Usage and CPU Load ------- 
Here is the standard output of the command: 

Filesystem     Size Used Avail Use% Mounted on 
/dev/mapper/ubuntu--vg-root 19G 16G 2.3G 88%/
udev       987M 4.0K 987M 1% /dev 
tmpfs      200M 268K 200M 1% /run 
none       5.0M  0 5.0M 0% /run/lock 
none       998M  0 998M 0% /run/shm 
/dev/sda1     236M 33M 191M 15% /boot 
Here is the standard error of the command (if any): 

我已經通過Java代碼如下行執行

String diskUsageCommandTest = "df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\n\", $3,$2,$5}'"; 

在那裏我得到以下輸出:

------- Track Memory Usage,Disk Usage and CPU Load ------- 
Here is the standard output of the command: 

Here is the standard error of the command (if any): 

df: `|': No such file or directory 
df: `awk': No such file or directory 
df: `\'$NF=="/"{printf': No such file or directory 
df: `"Disk': No such file or directory 
df: `Usage:': No such file or directory 
df: `%d/%dGB': No such file or directory 
df: `(%s)': No such file or directory 
df: `",': No such file or directory 
df: `$3,$2,$5}\'': No such file or directory 
df: no file systems processed 

在Linux終端我運行下面的命令:

df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}' 

在那裏我得到以下輸出:

Disk Usage: 16/19GB (88%) 

因此,任何人知道我可以在Java代碼中做的就是輸出格式如「磁盤使用率:16/19GB(88%)」?

回答

0

,你可以去看看here,這將解決您的問題 - 如果有可能你的情況(創建你的命令的shell腳本並執行一個)

0

Java的高管只需要一個命令,當你給所有管道都會嘗試找到名稱爲df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\n\", $3,$2,$5}'";的命令,該命令不存在(在shell中嘗試一下自己,在雙引號內輸入命令)。

所以你需要的是用這個命令創建一個shell腳本或執行例如bash -c "your command"

Process proc = Runtime.getRuntime().exec("bash", "-c", diskUsageCommandTest); 
+0

我添加了\\ n而不是\ n,並使用Process proc = Runtime.getRuntime().exec(「bash」,「-c」,diskUsageCommandTest);謝謝你的工作。 –

0

也許你不需要爲它調用shell命令。先看看下面的代碼片段。

double freeSpace = Math.round(root.getFreeSpace()/1024D/1024/1024); 
double totalSpace = Math.round(root.getTotalSpace()/1024D/1024/1024); 
long usedSpace = (long) (totalSpace - freeSpace); 
int percentage = (int) ((usedSpace * 100)/totalSpace); 
System.out.printf("Disk Usage: %d/%dGB (%d%%)%n", 
     usedSpace, 
     (int) totalSpace, 
     percentage 
); 

注意:該代碼被寫爲scratch。看看這個舍入是否適合你。