LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-05-2009, 10:19 PM   #1
scbops
Member
 
Registered: Feb 2006
Posts: 97

Rep: Reputation: 15
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 }'
 
Old 01-05-2009, 10:24 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You posted a question AND the answer---what am I missing?????
 
Old 01-05-2009, 10:49 PM   #3
scbops
Member
 
Registered: Feb 2006
Posts: 97

Original Poster
Rep: Reputation: 15
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
 
Old 01-05-2009, 11:10 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 01-05-2009, 11:36 PM   #5
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 01-05-2009, 11:54 PM   #6
scbops
Member
 
Registered: Feb 2006
Posts: 97

Original Poster
Rep: Reputation: 15
How should collect total CPU average at a given time ?
 
Old 01-06-2009, 12:01 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by scbops View Post
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.
 
Old 01-06-2009, 12:58 AM   #8
scbops
Member
 
Registered: Feb 2006
Posts: 97

Original Poster
Rep: Reputation: 15
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
 
Old 01-06-2009, 12:59 AM   #9
scbops
Member
 
Registered: Feb 2006
Posts: 97

Original Poster
Rep: Reputation: 15
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
 
Old 01-06-2009, 02:55 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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)
 
Old 01-06-2009, 08:08 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Sorry--I missed something. I thought that "top" gave the desired total(s). What does "man top" tell you?
 
Old 01-06-2009, 11:36 AM   #12
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 01-07-2009, 01:28 AM   #13
scbops
Member
 
Registered: Feb 2006
Posts: 97

Original Poster
Rep: Reputation: 15
top -b -n1 | awk ' BEGIN { total=0} { total+=$9} END {print total}'

Please be kind enough to explain how this command ex cutting ?
 
Old 01-07-2009, 01:50 AM   #14
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.

Last edited by ta0kira; 01-07-2009 at 01:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Generate alert with "CPU load average" "CPU utilization" viky Linux - Hardware 3 07-25-2007 04:24 AM
Alert used "cpu load" and "cpu utilization" viky General 0 07-23-2007 03:19 AM
Viewing 132 column using "less" command kvvkumar Linux - Software 2 05-11-2007 10:37 PM
Cpu "hlt" command and Mandrake 9.2 RC2, how do I get my laptop to run cooler? adamis Mandriva 2 10-26-2003 01:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration