Fedora Linux Support Community & Resources Center
  #1  
Old 16th December 2011, 01:03 AM
Bazu135 Offline
Registered User
 
Join Date: Aug 2011
Location: Luton, UK
Age: 28
Posts: 291
linuxfirefox
grep and awk in console

I'm attempting to write a mini-program/script/console command/not-sure-what-to-call-it that will record the CPU and memory usage of a given process, mostly just to teach myself how to use the functions involved (though if I manage it, it will potentially be useful in some coursework later in the year). Taking it in baby-steps, I'm currently trying to work out a console command that will display the details in the console.

My current thinking is that I can use top, grep the process name, and use awk to pick out the details I want - for now, just the raw numbers.
Code:
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
 2739 neko      20   0  479m  64m  33m S  6.0  1.8  29:26.77 clementine
Based on the above, my thinking is that I want to do top | grep clementine | awk '{print $9, $10}' to achieve it. Doing this just returns a blank line, however.

Any suggestions as to what I should be doing instead?
Reply With Quote
  #2  
Old 16th December 2011, 02:36 AM
PabloTwo Online
"Registered User" T-Shirt Winner
 
Join Date: Mar 2007
Location: Seville, FL
Posts: 6,137
linuxfirefox
Re: grep and awk in console

Top is a running process until you top it with CTRL-C. Try using the ps command to find (and pick out) what you want.
Code:
BASH:~/-> FINDIT=$(pidof conky) && ps -p $FINDIT -o %C -o %mem -o %c
%CPU %MEM COMMAND
 1.6  0.1 conky
And if you don't want the headers,
Code:
BASH:~/-> FINDIT=$(pidof conky) && ps --no-headers -p $FINDIT -o %C -o %mem -o %c
 1.6  0.1 conky
Then pipe the output into awk and do with it as you like, or arrange the output of of the ps command so it's already just what you want without having to use awk at all. There are endless possibilities here.
Reply With Quote
  #3  
Old 16th December 2011, 11:32 AM
Bazu135 Offline
Registered User
 
Join Date: Aug 2011
Location: Luton, UK
Age: 28
Posts: 291
linuxfirefox
Re: grep and awk in console

My intention was to do it using top so that I can get a constant stream of output - the aim being that I can track the process' resource usage for as long as it runs (i.e. so that I can find its peak CPU usage, lowest usage, mean usage etc) - really should've said that in the first post; that's what happens when posting at 1am!

If I run top | grep [process] then I get a repeated output line with only [process]'s details being shown, e.g.:
Code:
$ top | grep clementine
 2679 neko      20   0  356m  63m  30m S  7.9  1.8  11:28.22 clementine         
 2679 neko      20   0  356m  63m  30m S  7.0  1.8  11:28.43 clementine         
 2679 neko      20   0  356m  63m  30m S  7.0  1.8  11:28.64 clementine         
 2679 neko      20   0  356m  63m  30m S  7.0  1.8  11:28.85 clementine         
 2679 neko      20   0  356m  63m  30m S  6.6  1.8  11:29.05 clementine         
 2679 neko      20   0  356m  63m  30m S  7.0  1.8  11:29.26 clementine
From the above, I reasoned if I could get it to only display fields 9 and 10, I could output those to a file and work from there. Yours certainly provides a way to get only that output (and I learnt something new, so thank you!), but it only provides the output once. Is there a way to have that execute repeatedly - until I manually halt it, for instance?
Reply With Quote
  #4  
Old 16th December 2011, 12:16 PM
aesir Offline
Registered User
 
Join Date: Nov 2008
Location: ~
Age: 28
Posts: 175
linuxfirefox
Re: grep and awk in console

you can do loops in bash, for example

Code:
while sleep 1s; do
 ps ....
done
Reply With Quote
  #5  
Old 16th December 2011, 06:27 PM
Bazu135 Offline
Registered User
 
Join Date: Aug 2011
Location: Luton, UK
Age: 28
Posts: 291
linuxfirefox
Re: grep and awk in console

That loop works brilliantly - thank you!

Out of curiosity, running that loop (at 1sec intervals) gave me very different results to what having top running at the same time gave me - monitoring Firefox while I browsed a few sites gave me CPU values ranging from 6.6% to 7.3% in the loop; watching the Firefox process in Conky (which uses top, again at 1sec intervals) gave me values occasionally as high as 12.4% or 14.3%. Is there any reason that one might see this discrepancy - do they monitor a different value, for instance?

On the face of it, I'm more inclined to believe the top values show what I'm looking for, since they spiked when the page was loading and dropped once it had fallen again, while the loop gave a constant slow increase only (6.6, 6.6, 6.6, 6.7, 6.7, 6.7, 6.7, 6.8, 6.9, 6.9 etc). If that is the case, it rather leaves me back at square one - how would I take only those values as an output from top?

(Apologies for being picky!)
Reply With Quote
Reply

Tags
awk, console, grep

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Force text console as primary console gbuergisser Using Fedora 0 23rd November 2011 02:38 PM
Need help using grep, need to grep something from a txt file. qsub Using Fedora 6 9th January 2010 10:28 PM
"grep" username in samba or grep file owner? overlord Servers & Networking 0 22nd July 2005 02:12 PM
C++ Windows console to Linux Console porting Gav Programming & Packaging 4 14th June 2005 06:11 PM


Current GMT-time: 21:52 (Friday, 19-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat