6

I'm currently using

top -b -d 1 > file1.csv

to append the entire output to a csv file. However, I'd like the Cpu(%us) field alone to be entered in file1. I've gotten as far as this:

top -b -d 1|grep Cpu 

where I'm able to view only the Cpu entry.

When I try

top -b -d 1|grep Cpu > file1.csv

it doesn't deem to work as I'm not even able to view my file.

My intention is to plot a LiveGraph for all the CPU values (stored in a csv file) dynamically. Thanks!

  • Update- I'm able to top -b -d 1 > file1.csv (store it in a file) cat file1.csv | grep Cpu | cut -c 35-39 | nl > cpu.csv But the date doesn't get updated dynamically as I need to stop top command to execute the next grep. – P Ramesh Oct 18 '11 at 15:00
  • Why isn't this not working(Piping both commands to make cpu.csv get updated dynamically.I'm unable to even open the cpu.csv file)- top -b -d 1 > file1.csv | grep Cpu | cut -c 35-39 | nl > cpu.csv Can someone pls help me? – P Ramesh Oct 18 '11 at 15:09
6

For total CPU usage:

If you ONLY want the CPU Usage in general you might try this:

top -b -d1 -n1|grep -i "Cpu(s)"|head -c21|cut -d ' ' -f3|cut -d '%' -f1 > file1.csv

This will give you only the CPU value and update the one in the file. If you want to APPEND the data to the file (since I see you are naming it a csv file) then instead of one > use two, like >> file1.csv.

For each thread and CPU usage:

First you need to eliminate the rest of the columns so it is easier to get the CPU data.

  1. Run top and press f.
  2. In this menu you select which columns you want to see and which you do not. For your case leave only the CPU column and name (if you want the name)
  3. Press ESC to go back to the main Top menu and save with Capital W the change. Now you command is easier to parse.

Now for general CPU usage you normally have other tools like ps, iostat an doing a cat /proc/stat. For each you need a different parse like the one used for top. I only posted here for TOP since you explicitly mention it in the title.

NOTE: Should be noted that top is not the most efficient way to see the CPU usage or to work with when parsing values for it. For some, using f2 in the cut command shows the value, for others the f3

enter image description here

  • top -b -d1 -n1|grep -i "Cpu(s)"|head -c21|cut -d ' ' -f2|cut -d '%' -f1 > file1.csv this works currently......not top -b -d1 -n1|grep -i "Cpu(s)"|head -c21|cut -d ' ' -f3|cut -d '%' -f1 > file1.csv it gives a blank space. – piyushj Sep 29 '16 at 10:36
  • @piyushj Weird, for me on 16.04 64 bit, the f3 approach is the one that gives me a correct value, the f2 gives an empty one. I will upload an image. – Luis Alvarado Sep 29 '16 at 14:40
  • For me, on centos 6.8 and ubuntu 14.04.4 giving the result for f2........ may be its build and LINUX type dependent. – piyushj Sep 30 '16 at 3:18
  • @piyushj Sorry but I do not know why you got that. I tested 13.04, 13.10, 14.04(.0.1.2.3.4.5), 14.10, 15.04, 15.10, 16.04 and 16.10 beta to see where this different came from. On ALL cases, in every single one, F3 was the correct one, not the F2, you are mentioning. I do not know about CentOS but at least on all Ubuntu versions from 13.04 up to 16.10 they all work with F3 and not with F2. I also tested to see if it was a cpu issue but it was the same result with AMD and Intel, from Intel Core 2 Duo up to I7 6800K and from AMD Sempron & Opteron to FX-8120. – Luis Alvarado Sep 30 '16 at 14:21
2

I'm thinking that using top may not be the best approach. I would look at using /proc/stat instead. I found an article called "Calculating CPU Usage from /proc/stat" that may just solve most of the problem for you.

2

It takes a little while, because there seems to be a buffer that needs to be filled up before the pipes start to work.

Try with a small number of iterations first (note: -n2 means 2 iterations):

top -b -d1 -n2 | grep Cpu | cut -c 35-39

Regarding to your comment: If you redirect standard output to a file with top -b -d 1 > file.csv, you cannot pipe standard output into the grep command.

See:

echo "Standard output" | grep "out"

vs.

echo "Standard output" >/tmp/foo | grep "out"

In the second case you do not have any output.

0
top -b -n1 | grep Cpu | sed -r 's@.+:\s([0-9\.]+).+@\1@'

As has already been said the option -b together with -n 1 will give a single text output from top rather than the constantly updating default. Then you can pipe to sed to find the value you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.