I have a Fedora Core 8 box I ran these on, it should work for Unix and Linux systems:
This command pipes the cleansed output of 'top' to command line and cleans the escape codes.
My results:
top - 15:26:24 up 7:10, 3 users, load average: 0.18, 0.23, 0.28
Tasks: 223 total, 2 running, 220 sleeping, 0 stopped, 1 zombie
Cpu(s): 7.2%us, 1.8%sy, 0.4%ni, 89.6%id, 0.9%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 6219640k total, 5871364k used, 348276k free, 190064k buffers
Swap: 2031608k total, 0k used, 2031608k free, 3871044k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3812 el 20 0 364m 130m 31m S 10 2.2 7:28.10 /usr/lib/firefox-3.0/firefox-bin
2323 java 20 0 2061m 20m 18m S 4 0.3 0:06.92 ora_ckpt_kurdev
6277 el 20 0 35116 4196 3372 S 4 0.1 1:20.36 /usr/bin/pulseaudio -D
...
...
Use this to pipe the cleansed output of 'top' to command line and cut off the unnecessary header lines.
My Results:
3812 el 20 0 364m 130m 31m S 7 2.1 8:32.04 /usr/lib/firefox-3.0/firefox-bin
6277 el 20 0 35116 4196 3372 S 7 0.1 1:34.07 /usr/bin/pulseaudio -D
6051 root 20 0 379m 34m 21m S 2 0.6 12:15.65 /usr/bin/X -br -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-7jJzWo
21044 el 20 0 2532 996 712 R 2 0.0 0:00.02 top -cSb n 1
...
...
How to get the top output for only one pid (process id).
My results:
3812 el 20 0 364m 130m 31m S 10 2.2 9:01.30 /usr/lib/firefox-3.0/firefox-bin
How to track the cpu usage for a single process in Linux/Unix:
*Important: change the process id '3812' to one of your PID's.
My Results:
cpu % 8
For some reason the 'ps' command does not come out with the same results as top does (based on my observation). Top yields the most accurate summary of how much CPU your process is using.
You could also use ps to figure out how much total CPU load in Linux/Unix.
I make no guarantees for this command, it seems to match up with top's version, however if you have a dual core, or more than one core, this number returned will go to 200%. It's a hack.
My Results:
cpu % 24.3
This was the command that satisfied my problem:
replace 12345 with your PID:
/usr/bin/top -cSb -p 12345 n 1 | /usr/bin/tail -n+8 | /bin/sort -rn -k5 | /usr/bin/awk 'NR > 0 { s +=$9 }; END {print s}'
:rolleyes::)