-1

I want to find idle percentage of processor but it is showing all procinfo output

Process p = Runtime.getRuntime().exec("procinfo | grep idle");
while((s=stdInput.readLine())!=null) {
    System.out.println(s);
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user2095748
  • 267
  • 1
  • 5
  • 15

1 Answers1

1
import java.io.InputStream;

public class ProcInfo
{
    public static void main(String args[])
    {
        String[] command = { "/bin/sh", "-c", "procinfo | grep -oP '^idle.* \\K[0-9.]+%'" };
        try
        {
            Process p = Runtime.getRuntime().exec(command);
            p.waitFor();

            InputStream i = p.getInputStream();
            byte[] b = new byte[16];
            i.read(b, 0, b.length);
            System.out.println(new String(b));
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

Special Thanks to Cyrus for the command.

Darshan Patel
  • 2,839
  • 2
  • 25
  • 38