简体   繁体   English

如何使“ top”命令在Java运行时中工作,以返回进程列表以及每个进程消耗的资源量?

[英]How do you get the 'top' command to work in Java runtime to return a list of processes and the amount of resources each are consuming?

This is kind of a newb question, so please forgive me, I am a newb. 这是一个newb问题,所以请原谅我,我是newb。 Before posting I have checked all x billion pages on the Internet and there is not answer to my specific question. 发布之前,我已经检查了Internet上的所有十亿个页面,但没有针对我的特定问题的答案。 Okay not really, but it's been a good four hours of desk punching so I feel it is a good time to defer to the vast and seemingly all encompassing knowledge of the SE user base. 好的,不是,但是这是一个很好的四个小时的办公桌打卡工作,所以我觉得现在是顺应SE用户基础的广泛且看似全部包含的知识的好时机。 So, without further ado... here is my question. 因此,事不宜迟...这是我的问题。

As stated above, I am working with bash (OSX) and Java, and trying to get a script to run via process-builder (and/or Java runtime) IN THE EXACT SAME WAY that it would run by running it in the shell alone. 如上所述,我正在使用bash(OSX)和Java,并试图以完全相同的方式使脚本能够通过process-builder(和/或Java运行时)运行,而该脚本仅在shell中运行即可。

for example, if I write a shell script (.sh) containg the code: 例如,如果我编写一个包含以下代码的shell脚本(.sh):

"#!/bin/bash" “#!/斌/庆典”
"top > /Users/NN2/Desktop/test.txt" “顶部> / Users / NN2 /桌面/test.txt”

the file runs fine, and the expected output is created. 该文件运行正常,并创建了预期的输出。 However, if I do the following: 但是,如果我执行以下操作:

(excluded try/catch in example only) (仅在示例中排除了try / catch)
ProcessBuilder builder = new ProcessBuilder(new String[]{"/bin/sh","/Users/NN2/Desktop/configure.sh"}); ProcessBuilder builder =新的ProcessBuilder(新的String [] {“ / bin / sh”,“ / Users / NN2 / Desktop / configure.sh”});

or 要么

Runtime.getRuntime().exec("/Users/NN2/Desktop/configure.sh"); 。调用Runtime.getRuntime()EXEC( “/用户/ NN2 /桌面/ configure.sh”);

Either nothing happens, or a truncated version of the original appears, which does not include the #MREG info. 没有任何反应,或者出现了原始的截短版本,其中不包含#MREG信息。 In addition to the above two examples, I have tried what seems like an exhaustive list of other variations and it is always the same, either truncated or nothing. 除了以上两个示例,我还尝试了其他变体的详尽列表,并且总是相同的,要么被截断要么什么都没有。 Furthermore, this problem seems to only be with the "top" command, as most other commands I have tried do indeed work (ex: ls, ef aux, etc..). 此外,此问题似乎仅与“ top”命令有关,因为我尝试过的大多数其他命令确实可以工作(例如:ls,ef aux等)。

I have read that other people have had permission issues with the top command from within Java, however, I have used the chmod command on several other installations so this is likely not the cause of the problem either. 我读到其他人在Java中对top命令有权限问题,但是,我在其他几个安装中都使用了chmod命令,因此这也可能不是问题的原因。 However, if it is, then it is surely due to not knowing which files need to be chmod'ed. 但是,如果是这样,则肯定是由于不知道需要对哪些文件进行chmod。

Also, if there is any other way to find the information I need besides what I am doing please do tell. 此外,如果除了我正在做的事情以外,还有其他方法可以找到我需要的信息,请告诉我。 In the end, I just want to be able to use java to check what process are running on my computer, and how much memory they have used (to tell when a program has finished loading) before I do other things. 最后,我只想能够使用Java检查计算机上正在运行的进程,以及在执行其他操作之前它们已使用了多少内存(以告知程序何时完成加载)。 Different file sizes open at different durations, so thread.sleep command is a work-around that I do not wish to use. 不同的文件大小会在不同的持续时间打开,因此thread.sleep命令是我不希望使用的变通方法。

Thank you 谢谢

I tired something like... 我累了...

ProcessBuilder pb = new ProcessBuilder("top", "-l", "1");
pb.redirectError();
try {
    Process p = pb.start();
    InputStream is = p.getInputStream();
    int value = -1;
    while ((value = is.read()) != -1) {
        System.out.print(((char)value));
    }
    int exitCode = p.waitFor();
    System.out.println("Top exited with " + exitCode);
} catch (IOException exp) {
    exp.printStackTrace();
} catch (InterruptedException ex) {
    Logger.getLogger(JavaApplication256.class.getName()).log(Level.SEVERE, null, ex);
}

And it output something like... 它输出类似...

PID    COMMAND          %CPU TIME     #TH   #WQ #PORTS MEM    PURG   CMPRS  PGRP  PPID  STATE    BOOSTS      %CPU_ME %CPU_OTHRS UID FAULTS     COW        MSGSENT    MSGRECV    SYSBSD      SYSMACH   CSW         PAGEINS  IDLEW     POWER USER            #MREGS RPRVT VPRVT VSIZE KPRVT KSHRD
96500  USBAgent         0.0  00:00.02 2     1   34+    380K+  0B     720K+  96500 1     sleeping *0[1+]      0.00000 0.00000    501 2044+      135+       151+       55+        786+        310+      287+        0        9         0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
96499  smd              0.0  00:00.01 2     1   19+    8192B+ 0B     808K+  96499 1     sleeping *0[1+]      0.00000 0.00000    0   1066+      110+       73+        30+        430+        158+      146+        0        3         0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
84070  DiskUnmountWatch 0.0  00:00.02 2     1   25+    8192B+ 0B     852K+  84070 1     sleeping  0[0]       0.00000 0.00000    501 1290+      115+       201+       95+        1840+       313+      367+        0        5         0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
83951  ath              0.0  00:11.89 6     0   52+    4104K+ 0B     24M+   83850 83850 sleeping *0[14+]     0.00000 0.00000    501 14309+     152+       7429+      3385+      246813+     40467+    104382+     47+      821       0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
83930  com.apple.iTunes 0.0  00:26.91 2     1   45+    9736K+ 0B     76M+   83930 1     sleeping *0[6+]      0.00000 0.00000    501 101584+    149+       742302+    278377+    1094603+    464425+   387242+     49+      15        0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
83911  AppleMobileDevic 0.0  00:00.14 3     0   47+    24K+   0B     4500K+ 83850 83850 sleeping *0[10+]     0.00000 0.00000    501 3416+      202+       677+       298+       3344+       1005+     1292+       71+      21        0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
83907  com.apple.MediaL 0.0  02:04.68 2     0   85+    60M+   0B     75M+   83907 1     sleeping *0[41+]     0.00000 0.00000    501 215329+    1028+      763618+    373231+    3447492+    1274798+  1276787+    11429+   128       0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
83866  ath              0.0  00:06.18 4     0   40+    2120K+ 0B     13M+   83850 83850 sleeping *0[10+]     0.00000 0.00000    501 8056+      152+       5462+      2504+      136223+     31146+    37582+      3+       604       0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
83854  com.apple.BKAgen 0.0  00:00.04 2     1   42+    20K+   0B     1464K+ 83854 1     sleeping *0[2+]      0.00000 0.00000    501 2775+      171+       218+       93+        878+        469+      459+        37+      9         0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
.
.
.
29     thermald         0.0  00:00.60 2     0   31+    576K+  0B     544K+  29    1     sleeping *0[1+]      0.00000 0.00000    0   8021+      139+       12033+     6007+      28656+      16489+    12726+      52+      1009      0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
27     fseventsd        0.0  06:03.81 9     0   219+   8384K+ 0B     4168K+ 27    1     sleeping *0[1+]      0.00000 0.00000    0   1324890+   88+        673284+    16997+     20680415+   3283315+  5048419+    18+      550698    0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
26     kextd            0.0  00:49.92 2     0   58+    4628K+ 0B     13M+   26    1     sleeping *0[1+]      0.00000 0.00000    0   106864+    563+       10668+     6440+      431944+     36727+    136669+     2775+    245       0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
25     com.bombich.ccch 0.0  00:00.87 2     0   66+    1108K+ 0B     3068K+ 25    1     sleeping *0[1+]      0.00000 0.00000    0   10385+     221+       8129+      5704+      25753+      27131+    16864+      243+     282       0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
24     EyeConnect       0.0  01:55.37 12    0   61+    6964K+ 0B     4556K+ 24    1     sleeping *0[1+]      0.00000 0.00000    0   12222+     260+       1747+      1180+      3097895+    2482+     1264730+    892+     466746    0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
22     UserEventAgent   0.0  03:08.20 4     1   1439+  6220K+ 0B     2336K+ 22    1     sleeping *0[1+]      0.00000 0.00000    0   1136602+   295+       3259840+   1922426+   8525137+    6153879+  5940157+    1529+    141344    0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
21     syslogd          0.0  01:07.95 7     6   183+   956K+  0B     256K+  21    1     stuck    *0[116596+] 0.00000 0.00000    0   304083+    72+        1247136+   424254+    4546991+    1774131+  1223614+    10+      22760     0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
1      launchd          0.0  22:36.73 6     4   3547+  9800K+ 0B     3524K+ 1     0     sleeping  0[0]       0.00000 0.00000    0   158340+    6059+      18904581+  9244535+   30306022+   32624561+ 12693124+   1172+    42752     0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
0      kernel_task      0.0  20:23:16 100/4 0   2+     686M+  0B     0B     0     0     running   0[0]       0.00000 0.00000    0   25398+     0          258323633+ 231211103+ 0           0         2322888249+ 0        708699634 0.0   ...             N/A    N/A   N/A   N/A   N/A   N/A  
Top exited with 0

Now, obviously, you'll need to parse the individual lines to get meaningful information, but no more then you would have to by reading the output file... 现在,显然,您需要解析各行以获得有意义的信息,但是您无需再读取输出文件了……

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

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