Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Monitor one task's CPU usage via top UNIX
Yesterday, I was trying to figure out the best way to keep an eye on the CPU usage of one process (the Finder in this case). There are obviously a number of ways to do this -- using Activity Monitor was the first one that came to mind, but it's got a pretty large window and I wanted to work on other stuff while watching the Finder's CPU usage.

So then I went to the Terminal and spent a bit of time looking at top's options. top will let you run a given number of samples with the -l option, and if you specify 0 as the sample count, you'll get an infinite number of samples. Combine this with grep to isolate the Finder from top's output, and you get the following command and output, updated every second.
robg$ top -l 0 | grep Finder
  406 Finder       0.0%  0:19.09   1   134   209  7.45M  37.4M  25.0M   267M 
  406 Finder       0.8%  0:19.10   2   139   212  7.46M+ 38.9M+ 25.1M+  268M+
  406 Finder       7.2%  0:19.19   2   145   212  7.48M+ 38.9M+ 25.2M+  268M+
  406 Finder       9.2%  0:19.30   2   142   213  7.48M  39.6M+ 25.7M+  269M+
  406 Finder       6.6%  0:19.38   2   142   211  7.47M- 39.0M- 25.2M-  268M-
^C
So there's one relatively trivial solution -- I then resized the Terminal window to a very small size, and just let that scroll by for a while as I worked.

I still wasn't quite happy, though, as this still took up a fair bit of screen real estate, and I had to be sure to make a new Terminal window when I wanted to use Terminal itself. The image at left is more like what I was after ... read the rest of the hint to see how it was created after some detective work by myself and Kirk McElhearn...

I thought that GeekTool (a previous Pick of the Week selection) might be a perfect answer to this problem, as it can output Terminal commands to floating desktop windows. Ideally, I'd just be able to tell GeekTool to run top -l 1 | grep 'Finder', set that to auto-update every second, and be done with. Unforunately, top -l 1 will always return a 0.0% utilization on the one sample it runs; it's only the second and subsequent samples that return the actual utilization. So I needed a way to ignore the first reply, and focus only on the second.

There were some other issues as well. I didn't want nor need the extraneous memory usage information; I was just interested in CPU usage. And the top command by itself is very CPU intensive, so you'll be loading your CPU if you run the command every second or two. So Kirk and I spent a bit of time this morning trying to put this all together to come up with the ultimate one-line GeekTool single-process monitoring tool. After some trial and error, here's what we wound up with to generate the image you saw above:
top -FR -l2 | grep Finder | grep -v 0.0% | cut -c 7-25
Here's how each piece works:
  • top: Launch the top command, which shows all running processes and a bunch of information about each one.
  • -FR: The F option tells top not to calculate stats on frameworks; the R option tells top not to report on the 'memory object map' for each process. The net of both of these options is that top consumes much less CPU than it does in its normal mode.
  • -l 2: Run two samples in top. The first will always return 0.0%, so we need two.
  • | grep Finder: Only show output containing the word Finder.
  • | grep -v 0.0%: Skip any rows containing 0.0%. This effectively skips row number one (and yes, any time the Finder is at 0.0% actual usage, you'll get a blank display in GeekTool).
  • | cut -c 7-25: This shows only columns 7 through 25 of the output, which are those containing the word Finder and the utilization number.
There are, of course, many improvements that could be made -- you don't have to use the Finder as the process (you could even search on process numbers if you wished); you don't have to use a display window in GeekTool (then you could actually place the one line on your menubar, for instance); and there very well may be a more efficient way to do this (anyone?). But this works for me, and helped me keep an eye on the Finder to see what specific tasks were taking the most CPU time.
    •    
  • Currently 1.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[27,133 views]  

Monitor one task's CPU usage via top | 16 comments | Create New Account
Click here to return to the 'Monitor one task's CPU usage via top' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
geektool is great, but also is MenuMeters
Authored by: metiure on Nov 16, '04 02:41:08PM

Simply use MenuMeters to monitor your system and forget about 'top'.

Vic



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: eduo on Nov 16, '04 03:24:33PM

This is what I use:
ps -acx -o %cpu,command | sort -rin | head -n1

---
Eduo



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: eduo on Nov 16, '04 03:26:51PM

I should add that this could easily be changed to show the topmost 'n' CPU-intensive apps by changing the value of 'head -n', right now it shows the topmost (1).

And I'm confident top could be configured to display only one line ordered by cpu usage but I didn't look further..:)

Eduo

---
Eduo



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: kirkmc on Nov 16, '04 03:42:01PM

Yes, that's top -ocpu -n 1, to display just one process, but it also displays all the lines with the summary info at the top of the window. So you could grep to find only those lines with processes, though it doesn't look that simple - there is no common text in all the process lines that is not in the summary.

---
Read my blog: Kirkville -- http://www.mcelhearn.com
Musings, Opinion and Miscellanea, on Macs, iPods and more



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: phil4u2 on Nov 16, '04 04:13:09PM
Just a mistake on your command, you forgot the brackets (') it should read:
top -FR -l2 | grep 'Finder' | grep -v '0.0%' | cut -c 7-25

[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: kirkmc on Nov 17, '04 04:22:55AM

You mean single quotes, not brackets. Brackets are this: [].

In any case, you don't need single quotes when greping a single word, and it works fine for 0.0% as well. But it's true that it is a good habit to develop, since the lack of quotes can break a lot of grep searches.

---
Read my blog: Kirkville -- http://www.mcelhearn.com
Musings, Opinion and Miscellanea, on Macs, iPods and more



[ Reply to This | # ]
Could somebody tell me how to create that 'window' in GeekTool?
Authored by: hamarkus on Nov 16, '04 04:56:34PM

Could somebody tell me how to create that 'window' in GeekTool? I select shell, then command, paste the line in there and then? I guess I have to check the box 'Enable GeekTool'. I can get the console output, and I had for brief moment the Finder processor usage in one browser window, but now nothing is displayed anymore.



[ Reply to This | # ]
Could somebody tell me how to create that 'window' in GeekTool?
Authored by: robg on Nov 16, '04 07:26:37PM
Here's a screenshot -- make sure you don't set the update rate too low, or you won't see anything at all.

You can then use the Colors & Fonts and Text sections to control how the output looks.

-rob.

[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: snwbrdr3448 on Nov 16, '04 05:46:45PM

here are some of the commands im using in geektool based on this hint:

load average:
top -u -FR -l1 | grep 'Load Avg' | cut -c 1-27
cpu usage:
top -u -FR -l1 | grep 'Load Avg' | cut -c 33-90
memory left:
top -u -FR -l1 | grep 'PhysMem' | cut -c 1-9 -c 65-80
currently running programs using the CPU:
top -u -FR -l2 | grep -v 0.0% | grep % | grep -v Load | grep -v COMMAND | cut -c 7-24



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: Whosawhatsis on Nov 17, '04 01:17:34AM

I will never understand why people do a bunch of greps in a row combined with other stuff when a single awk will do... well, at least they didn't throw an awk in with the other stuff...

---
I was offered a penny for my thoughts, so I gave my two cents... I got ripped off.



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: kirkmc on Nov 17, '04 04:24:52AM

Can you tell us how to do it with awk? I find awk unbearably confusing... Especially since you are dealing with multiple lines here.

But, hey, the joy of the command line is that there are so many different ways to get the same results!

---
Read my blog: Kirkville -- http://www.mcelhearn.com
Musings, Opinion and Miscellanea, on Macs, iPods and more



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: bimtob on Nov 17, '04 02:36:30PM
I prefer perl. Don't know awk regex :)
My geektools...

Uptime
uptime | perl -p -e 's/^(.+?up\s+?)(\d+.+?)(\,\s+\d+\s+user.+)(.*)$/Uptime: $2/'
Load
uptime | perl -p -e 's/^(.*?)(\d\.\d\d\s\d\.\d\d\s\d\.\d\d)$/Load Avgs: $2/'
I also wrote a calendar script that looks better than the default cal output, and use Geektool to put it on my desktop. OSX should have an option to show the date in the menu bar (without clicking), but until then I use this.

It prints:

   November 17 2004
  S  M Tu  W Th  F  S
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16[17]18 19 20
 21 22 23 24 25 26 27
 28 29 30
The script:

#!/usr/bin/perl

use POSIX qw/strftime/;
use integer;

$date = strftime "%e",localtime;
$date =~ s/\s//;
@cal = `cal`;
$cal[0] = (strftime "%B %e %Y", localtime)."\n";
$cal[0] =~ s/\s\s/ /;
$il = " " x ((length($cal[1]) - length($cal[0]))/2);
$cal[0] =~ s/^/ $il/; 
print $cal[0];
for ($i = 1; $i <=6; $i++) {
        $cal[$i] =~ s/^(.*)\n$/ $1/;
        $cal[$i] =~ s/(^|\s)$date(\s|$)/\[$date\]/;
        print $cal[$i]."\n";
}


[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: Felix_the_Mac on Nov 17, '04 06:02:50PM
Here's my attempt:

while (true); do clear;ps -c -p 1670  -o command -o %cpu | grep -v COMMAND ; sleep 1; done;
It will give you a continuous, single line output of the pid that you specify (1670 in the example) Marks out of 10? :-)

[ Reply to This | # ]
Just what I was looking for
Authored by: VRic on Nov 19, '04 05:16:41PM

Great ! I was precisely looking for a workaround for top -l returning 0% CPU on 1st pass this morning!

I would have liked to get the full 2nd pass of top -l2 including all processes, but I don't know how.

Maybe one could count the number of lines, divide by 2, then return the second half of the output, which is the full second iteration of top -l2. Does anyone know how to do this?

In the meantime, here's what I did using only processes with more than 0%CPU.

To extract several columns from top's output, I made a template where I numbered the character positions of each column. Those who want custom output may find this useful:

(the first line shows column boundaries marked with the | character, eg PID is characters 1 to 5, process name is 6 to 16 etc.)

|1 5|        16|    24|      32| 36|   42|                  63|    70|    77|
  PID COMMAND      %CPU   TIME   #TH #PRTS #MREGS RPRVT  RSHRD  RSIZE  VSIZE
16786 top          2.5%  0:00.52   1    19     0     0K-    0K-  736K+ 27.1M+
15899 X Resource   0.9%  1:17.68   1    76     0     0K-    0K- 4.71M   206M 
10790 iCab         5.7% 72:27.70   8   207     0     0K-    0K- 97.5M-  381M 
  913 BitTorrent   0.3%  3:26:26   3    96     0     0K-    0K- 11.2M   235M 
  732 VLC          1.5% 95:52.83   7   193     0     0K-    0K- 29.9M-  433M 
  431 EyeTV Help   0.9%  2:19.16   1    74     0     0K-    0K-  760K   191M 
  428 Butler       0.3%  5:07.16   3   116     0     0K-    0K- 5.34M   263M 
  399 Finder       0.9% 43:49.23   4   961     0     0K-    0K- 39.9M   506M 
  210 WindowServ   7.3%  3:05:44   2   553     0     0K-    0K-  108M+  365M+
    0 kernel_tas   4.1%  1:42:41  35     2     0     0K-    0K  77.9M+  756M 

Here's an example to get the first 3 colmuns (notice that I don't grep just '0.0%' like the original hint, because this may also match 10.0%, 20.0%, etc.):

top -FR -l2  | grep '^....[1234567890] ' | grep -v ' 0.0% ..:' | cut -c 1-24

16589 top          2.3% 
15899 X Resource   1.3% 
11578 Carracho 1   0.3% 
10790 iCab         8.9% 
  913 BitTorrent   0.3% 
  732 VLC          0.3% 
  399 Finder       0.3% 
  397 Dock         0.3% 
  210 WindowServ   3.7% 
    0 kernel_tas   2.0% 

And here's what I use to show PID, name, CPU, threads, parts and memory usage in GeekTool (ranges are separated by commas):

top -FR -l2 | grep '^....[1234567890] ' | grep -v ' 0.0% ..:' | cut -c 1-24,33-42,64-77

17249 top          2.4%    1    19  740K+ 27.1M+
16332 Terminal     0.4%    4    67 17.0M   222M 
15899 X Resource   0.8%    1    76 4.71M   206M 
11578 Carracho 1   0.4%    6   214 11.8M   297M 
10790 iCab         9.0%    8   207 93.7M-  381M 
  913 BitTorrent   0.8%    3    96 10.9M   235M 
  732 VLC          1.6%    7   193 28.6M   433M 
  437 System Eve   0.4%    1    72 1.74M   225M 
  432 ContourShu   0.4%    1    68  840K   200M 
  210 WindowServ   2.8%    2   564  103M+  363M+
    0 kernel_tas   1.6%   35     2 78.4M+  756M 


[ Reply to This | # ]
Just what I was looking for
Authored by: Dale Mox on May 11, '05 12:25:36AM

sweet. Thanks, Dale



[ Reply to This | # ]
Monitor one task's CPU usage via top
Authored by: loren_ryter on Sep 23, '09 03:54:23PM
I'm no expert here, but based on this hint I experimented in Snow Leopard, where top output seems to be formatted differently.

This is what works for me in case it is of use to anyone:

top -F -l2 | grep '^..[1234567890-]' | grep -v -e ' 0.0 ..:' -e '/../' -e ' top ' -e ' grep ' | cut -c 1-29,76-82,90-96,198-206

[ Reply to This | # ]