top -pid 3907 -stats "pid,command,cpu"
-pid 3907
: your process ID
-stats pid,command,cpu
: only show process ID, name and CPU%
No need to run awk
on the output.
If you want to post-process the output, use -l 0
to run in logging mode (0 means indefinitely, everything else limits number of samples). Output will look like this (two repetitions):
Processes: 72 total, 3 running, 1 stuck, 68 sleeping, 326 threads
2011/05/10 19:15:13
Load Avg: 0.14, 0.14, 0.09
CPU usage: 20.0% user, 26.66% sys, 53.33% idle
SharedLibs: 5304K resident, 5208K data, 0B linkedit.
MemRegions: 16345 total, 1048M resident, 54M private, 338M shared.
PhysMem: 726M wired, 1802M active, 687M inactive, 3215M used, 750M free.
VM: 169G vsize, 1036M framework vsize, 5261732(0) pageins, 552476(0) pageouts.
Networks: packets: 46747406/52G in, 32528901/3715M out.
Disks: 9452898/244G read, 11226269/293G written.
PID COMMAND %CPU
3907 WindowServer 0.0
Processes: 72 total, 3 running, 1 stuck, 68 sleeping, 326 threads
2011/05/10 19:15:14
Load Avg: 0.13, 0.14, 0.09
CPU usage: 0.95% user, 1.90% sys, 97.14% idle
SharedLibs: 5304K resident, 5208K data, 0B linkedit.
MemRegions: 16346 total, 906M resident, 54M private, 386M shared.
PhysMem: 726M wired, 1802M active, 687M inactive, 3215M used, 751M free.
VM: 169G vsize, 1036M framework vsize, 5261732(0) pageins, 552476(0) pageouts.
Networks: packets: 46747406/52G in, 32528901/3715M out.
Disks: 9452898/244G read, 11226269/293G written.
PID COMMAND %CPU
3907 WindowServer 2.7
Use awk
or a similar tool to only display every 13th line (as these lines contain the values in that example):
$ top -l 0 -pid 3907 -stats pid,command,cpu | awk 'NR%13==0'
3907 WindowServer 0.0
3907 WindowServer 1.3
3907 WindowServer 2.2