在procinfo输出上运行grep命令

时间:2014-08-31 06:01:44

标签: java grep

我想找到处理器的空闲百分比,但它显示所有procinfo输出

Process p = Runtime.getRuntime().exec("procinfo | grep idle");
while((s=stdInput.readLine())!=null) {
    System.out.println(s);
}

1 个答案:

答案 0 :(得分: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);
        }
    }
}

特别感谢 Cyrus 获取该命令。

相关问题