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.
- Run
top
and press f.
- 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)
- 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