LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-30-2008, 05:54 AM   #1
jdt05
LQ Newbie
 
Registered: Apr 2008
Posts: 7

Rep: Reputation: 0
Monitoring process CPU and Memory usage


Hi everyone,

I want to build a script that monitors CPU usage and memory of certain processes. I know which PIDs the processes have and the most important thing to me is the accuracy of the readings.

I want to produce an overall graph of memory usage and cpu usage for each of my processes.

Which is the best command/method to capture the data do you think?

Thanks for any help,
jT
 
Old 04-30-2008, 08:35 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
The ps command allows you to specify options as to which metrics you want as well as to specify which process to show with the -p flag. You can use the h flag to suppress print of the header.

So:
Code:
while true; do  ps h -p<pid> -o pid,ppid,user,group,%cpu,%mem,rss,sz,vsz,cmd ; sleep 5; done
Where <pid> is the process ID in which you're interested.

This is standard ps command but instead of using default "-ef" you specify
the options you want with "-o". As noted the h suppresses the header.

The options are separated by comma. The options shown above are:
pid Process ID number
ppid Parent process ID number
user Effective user name (owner of the process)
group Effective group name (group of the process)
%cpu Percent of CPU utilization
%mem Percent of memory utilization
rss Resident Set Size
sz Size in physical pages of the core image.
vsz Virtual memory size
cmd The command and its arguments. Putting it last in options gives full details. Putting it anywhere else in options gives an abbreviated output.

You can of course use only the %cpu and %mem to get those two basic metrics you requested.

You can also change the sleep to a number greater than 5 (seconds) depending on what measurement interval you wish. Do NOT eliminate the sleep entirely as it will kill your CPU.

Last edited by MensaWater; 04-30-2008 at 08:36 AM.
 
Old 04-30-2008, 11:39 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,813
Blog Entries: 54

Rep: Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988Reputation: 2988
Hmm. I'd try searching Freshmeat and Sourceforge for a monitoring application before giving in and writing a script. Chances are this particular wheel was already invented, maintained and supported.
 
Old 05-02-2008, 05:23 AM   #4
jdt05
LQ Newbie
 
Registered: Apr 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Problem is, I'm very limited on what is installed on these machines. Scripting is the only way to do it.

I've figured out what I need to do but I'm stuck at a another hurdle now, see my other post http://www.linuxquestions.org/questi...aiting-639291/
 
Old 05-02-2008, 08:48 AM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
If you "figured out" what you needed to do and it wasn't in one of the posts above you should post your solution so others searching the forum for similar question will find it and not have to ask.
 
Old 05-02-2008, 09:10 AM   #6
jdt05
LQ Newbie
 
Registered: Apr 2008
Posts: 7

Original Poster
Rep: Reputation: 0
So what I need to do was to remotely kick off a process and then monitor its memory and cpu usage.

So on my local machine the pseudocode would be as follows:-

Code:
RPID = rsh host mycommand&

while( true )
    result = rsh host ps -o pcpu,pmem -p RPID

    Parsed = myparser( result );
    
    AllResults &= Parsed;
    
    If( Parsed.Break )
        break;
   
    Sleep( 0.1 )

Return( AllResults )
jT
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
tool to measure cpu/memory usage by a process saritha Linux - Software 1 10-08-2007 07:16 AM
Process Cpu Usage Monitoring sanjay_smg Linux - Software 1 12-04-2006 06:53 AM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 09:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 05:45 AM
limiting cpu and memory usage by user process amitkush Linux - Security 2 07-04-2003 01:21 AM


All times are GMT -5. The time now is 07:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration