从java运行命令时输出不同的输出

时间:2014-07-29 20:21:55

标签: java shell

我有以下脚本:

#!/bin/bash
ID=$PPID
read PID < <(exec ps -o ppid= "$ID")
top -cbn 1 -p $PID
grep -f <(pstree -cp $PID | grep -Po '\(\K\d+'| sed -re 's/$/ /g' | sed -re 's/^/^\\s\*/g' ) <(top -cbn 1)

当我从命令提示符运行此脚本时,输出为

top - 16:43:17 up  6:40,  6 users,  load average: 0.04, 0.02, 0.00
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.6%sy,  0.0%ni, 98.1%id,  0.4%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4152800k total,  1908108k used,  2244692k free,    92984k buffers
Swap:  1048568k total,        0k used,  1048568k free,  1104756k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                         
16866 root      40   0  7792 3188 2088 S  0.0  0.1   0:00.01 su                                                                                                                                                                              

16750 builder   40   0 1213m 262m  15m S  0.0  6.5   0:55.43 /opt/ibm/java-i386-60/jre/bin/java -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx1024m -jar /home/builder/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.

注意COMMAND的输出,它显示完整的命令。但是,如果我从java运行相同的程序,输出截断COMMAND名称,我无法弄清楚为什么?

19 字符后发生截断。 或者用不同的词语 每行在80个字符后截断。

如果我使用Java

运行相同的程序,这是输出
top - 16:13:52 up  6:10,  6 users,  load average: 0.16, 0.16, 0.06
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.6%sy,  0.0%ni, 98.0%id,  0.4%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   4152800k total,  1913364k used,  2239436k free,    91560k buffers
Swap:  1048568k total,        0k used,  1048568k free,  1103952k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
16750 builder   40   0 1206m 257m  15m S  0.0  6.3   0:26.32 /opt/ibm/java-i386-

16750 builder   40   0 1206m 257m  15m S  1.9  6.3   0:26.33 /opt/ibm/java-i386-
16943 builder   20   0  2608 1008  748 R  1.9  0.0   0:00.02 top -cbn 1         
16918 builder   20   0  554m  14m 6060 S  0.0  0.4   0:00.10 /opt/ibm/java-i386-
16934 builder   20   0  4976 1120  992 S  0.0  0.0   0:00.00 /bin/bash /home/bui
16941 builder   20   0  4976  508  376 S  0.0  0.0   0:00.00 /bin/bash /home/bui
16942 builder   20   0  4388  888  608 S  0.0  0.0   0:00.00 grep -f /dev/fd/63

我运行命令的java文件是

public class InformationFetcher {
    public static void main(String[] args) {
    InformationFetcher informationFetcher = new InformationFetcher();
    try {
        Process process = Runtime.getRuntime().exec(
            informationFetcher.getFilePath());
        InputStream in = process.getInputStream();
        printInputStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

    private void processInformation(InputStream in) {
    topProcessor.process(in);
    }

    private static void printInputStream(InputStream in) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuffer outBuffer = new StringBuffer();
    String newLine = System.getProperty("line.separator");
    String line;
    while ((line = reader.readLine()) != null) {
        outBuffer.append(line);
        outBuffer.append(newLine);
    }
    System.out.println(outBuffer.toString());
    }

    public String getFilePath() {
    return this.getClass().getResource("/idFetcher.sh").getPath();
    }

}

0 个答案:

没有答案