LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Collection Values of "ps" command CPU column (https://www.linuxquestions.org/questions/programming-9/collection-values-of-ps-command-cpu-column-695230/)

scbops 01-05-2009 10:19 PM

Collection Values of "ps" command CPU column
 
How should I collect all values in $3 column of ps aux command.

ps aux |gawk ' { print $3 }'

pixellany 01-05-2009 10:24 PM

You posted a question AND the answer---what am I missing?????

scbops 01-05-2009 10:49 PM

I want get the total of CPU column

out put of ps command

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

db2inst1 7412 0.3 14.6 2102444 1220716 ? S 2008 1014:03 db2pfchr 0
db2inst1 7413 0.3 14.4 2102444 1198760 ? S 2008 827:52 db2pfchr 0
db2inst1 7414 0.2 14.3 2102444 1192684 ? S 2008 747:50 db2pfchr 0
db2inst1 7415 0.1 14.3 2102444 1191140 ? S 2008 490:54 db2pfchr 0
db2inst1 7416 0.0 14.2 2102460 1184528 ? S 2008 101:36 db2pclnr 0
db2inst1 7417 0.0 14.0 2102460 1171716 ? S 2008 101:03 db2pclnr 0
db2inst1 7418 0.0 14.1 2102460 1174420 ? S 2008 53:40 db2pclnr 0
db2inst1 7419 0.0 14.2 2102460 1187580 ? S 2008 99:30 db2pclnr 0
db2inst1 7420 0.0 14.2 2102460 1184460 ? S 2008 100:11 db2pclnr 0
db2inst1 7421 0.0 14.2 2102460 1186876 ? S 2008 100:45 db2pclnr 0
db2inst1 7422 0.0 14.2 2102460 1182888 ? S 2008 100:48 db2pclnr 0

pixellany 01-05-2009 11:10 PM

Take a look at "top". It gives the total CPU usage and the usage for each process. On my machine, the total DOES NOT match the sum of the various processes. My interpretation is that the CPU usage reported for each process is the average over a certain window. 2 processes, however, will not be using the cpu at the same time, so the total will be less than the sum of the averages.

I don't know of a command to sum up a series of number, but keep in mind that the output of something like ps is rounded--eg all those "0.0"s may really be some finite value.

bgeddy 01-05-2009 11:36 PM

You could try this but as said I think the result is pretty useless in the real world.

Code:

ps aux | awk 'BEGIN {total=0}
{ print $0
total+=$3 }
END { printf("Total of CPU :%d\n",total)} '

This will total up column three but the result doesn't seem much use.

scbops 01-05-2009 11:54 PM

How should collect total CPU average at a given time ?

pixellany 01-06-2009 12:01 AM

Quote:

Originally Posted by scbops (Post 3398481)
How should collect total CPU average at a given time ?

Did you look at "top" as I suggested? In particular, look at the -d option.

scbops 01-06-2009 12:58 AM

In top out how can get the CPU usage in to one value(can I add all values in CPU raw)

# top
top - 01:24:29 up 22:00, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 74 total, 1 running, 73 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.8%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1025716k total, 359408k used, 666308k free, 135128k buffers
Swap: 2031608k total, 0k used, 2031608k free, 176960k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2060 636 544 S 0.0 0.1 0:01.80 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
3 root 39 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0

scbops 01-06-2009 12:59 AM

In top out how can get the CPU usage in to one value(can I add all values in CPU raw)

# top
top - 01:24:29 up 22:00, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 74 total, 1 running, 73 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.8%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1025716k total, 359408k used, 666308k free, 135128k buffers
Swap: 2031608k total, 0k used, 2031608k free, 176960k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2060 636 544 S 0.0 0.1 0:01.80 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0
3 root 39 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0

chrism01 01-06-2009 02:55 AM

You might want to look at the -b and -n options to top. You have to decide which CPU values you want eg %us (user)

pixellany 01-06-2009 08:08 AM

Sorry--I missed something. I thought that "top" gave the desired total(s). What does "man top" tell you?

bgeddy 01-06-2009 11:36 AM

Try :
Code:

man sar
- may be useful..

To get the total from top :
Code:

top -b -n1 | awk ' BEGIN { total=0} { total+=$9} END {print total}'
It may help if you say what you are trying to achieve here. Please be specific if you can.

scbops 01-07-2009 01:28 AM

top -b -n1 | awk ' BEGIN { total=0} { total+=$9} END {print total}'

Please be kind enough to explain how this command ex cutting ?

ta0kira 01-07-2009 01:50 AM

Code:

{ ps aux | gawk ' { print $3 }' | grep -v CPU | sed '2,$ s/$/ +/'; echo p; } | dc
I hope you realize that totaling the values won't give you an accurate total; look at all of the zeros.

Mine probably isn't the best. What it does is removes the column heading first, then adds a + sign to the second thru last lines and feeds it to the dc program, which performs all of the additions. The p tells it to print the result when it's done.
ta0kira

PS If you can get a list with one number per line and only numbers, the underlined part will total it for you.


All times are GMT -5. The time now is 06:28 AM.