0

I have following script:

#!/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)

When I am running this script from command prompt the output is

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.

Notice the output of COMMAND and it is displaying the complete command. But if I run the same program from java, the output truncate the COMMAND name and I am not able to figure out why ?

Truncation occur after 19 character. Or in different words Every line truncate after 80 character.

Here is the output if I run the same program using 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

My java file to run the command is

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();
    }

}
Ashish
  • 14,295
  • 21
  • 82
  • 127
  • I guess you already debugged your program and checked if `line` contains the whole line or not? – Tom Jul 29 '14 at 20:32
  • @Tom Yes sir I have done that – Ashish Jul 29 '14 at 20:35
  • 1
    Good. A small research resulted in the information that the standard line length is set to 80 characters in the `BufferedReader` class. You should check how to increase that setting. – Tom Jul 29 '14 at 20:46
  • May bis could help you: http://stackoverflow.com/questions/5960554/maximum-line-length-for-bufferedreader-readline-in-java – Tom Jul 29 '14 at 20:50
  • Here, man page of top (version procps-ng version 3.3.8) says (for -w switch): `when used without an argument top will format output using the COLUMNS= and LINES= environment variables`. Have you tried setting environment variable `COLUMNS` or passing `-w NNN`? – halfbit Jul 29 '14 at 21:05
  • I could not find the -w option – Ashish Jul 29 '14 at 21:08
  • @halfbit top -c is displaying complete command name – Ashish Jul 29 '14 at 21:13
  • I see, on my box specifying both `-c` and `-w500` does the trick. What OS are you using? – halfbit Jul 29 '14 at 21:55
  • I am using redHat Linux – Ashish Jul 30 '14 at 14:06

0 Answers0