Running CPU Utilization command in Java in Linux -


i writing java program run in linux machine. wanted cpu utilization of system. getting cpu utilization of machine use command :

top -bn 2 -d 0.01 | grep '^cpu.s.' | tail -n 1 | gawk '{print $2+$4+$6}' 

then call command java code, not output. normal linux commands such ls works me.

my code is:

public class helloworld{       public static void main(string []args){          string s;         process p;         try {            p = runtime.getruntime().exec("top -bn 2 -d 0.01 | grep '^cpu.s.' | tail -n 1 | gawk '{print $2+$4+$6}'");               bufferedreader br = new bufferedreader(                 new inputstreamreader(p.getinputstream()));             while ((s = br.readline()) != null)                 system.out.println("line: " + s);              p.waitfor();             system.out.println ("exit: " + p.exitvalue());             p.destroy();         } catch (exception e) {}      } } 

to use pipes, need run command within shell:

processbuilder b = new processbuilder("/bin/sh", "-c",       "top -bn 2 -d 0.01 | grep '^cpu.s.' | tail -n 1 | gawk '{print $2+$4+$6}'");  p = b.start(); 

also, please aware command fails on different locale. , output of top may vary too.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -