I'm developing a small utility in Java, that monitors a remote Linux machine's cpu. I'm using using sar
command to monitor it. The problem is, when i write sar 1 1
it takes some time to get cpu usage. I tried sar 0
but it's showing same cpu values instead of online cpu usage. What can i do to take cpu usage instantly?
Asked
Active
Viewed 888 times
0

AloneInTheDark
- 938
- 4
- 15
- 36
1 Answers
1
One option would be:
top
or
top | grep <App_name>
or
ps -p <app_pid> -o %cpu
Edit
To obtain only the number of % use of cpu of a certain pid:
ps -p PID -o %cpu | sed 's/[^0-9]//g' | tr -d '\n'
or
ps -p PID -o %cpu | sed -n 2p | sed 's/ //'

Luca Davanzo
- 21,000
- 15
- 120
- 146
-
you need of the number without '%'? take a look at edit – Luca Davanzo Feb 26 '14 at 16:27
-
+1 for your efforts.what if i want total cpu usage? not by pid? – AloneInTheDark Feb 26 '14 at 19:40