-1

I'm working on the project where I've to track Disk Usage on Linux using java code.I've executed the following code

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

where I get the following output:

------- 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):

I've executed below line via java code

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

where I get the following output:

------- 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

On the linux terminal I run the following command :

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

where I get the following output :

Disk Usage: 16/19GB (88%)

So anyone know what Can I do in the java code to get the output in format like "Disk Usage: 16/19GB (88%)"?

Rupali Pawar
  • 37
  • 1
  • 9

3 Answers3

0

you can take a look here, that would solve your problem - if it's possible in your case (create a shellscript with your commands and execute that one)

Community
  • 1
  • 1
Halko Karr-Sajtarevic
  • 2,248
  • 1
  • 16
  • 14
0

Java exec expects a single command, when you provide it with all the pipes it tries to find the command named df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\n\", $3,$2,$5}'"; which doesn't exist (try it your self in your shell, enter your command inside double quotes).

So what you need is either create a shell script with this command or execute e.g. bash -c "your command":

Process proc = Runtime.getRuntime().exec("bash", "-c", diskUsageCommandTest);
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • I added \\n instead of only \n and used Process proc = Runtime.getRuntime().exec("bash", "-c", diskUsageCommandTest);Thank you it works. – Rupali Pawar Jun 15 '16 at 07:19
0

Maybe you don't need to call a shell command for it. Have a look at the below snippet to start with.

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

note: The code was written as scratch. Have a look if the rounding is ok for you.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69