LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Monitoring process CPU and Memory usage (https://www.linuxquestions.org/questions/programming-9/monitoring-process-cpu-and-memory-usage-638848/)

jdt05 04-30-2008 04:54 AM

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

MensaWater 04-30-2008 07:35 AM

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.

unSpawn 04-30-2008 10:39 AM

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.

jdt05 05-02-2008 04:23 AM

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/

MensaWater 05-02-2008 07:48 AM

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.

jdt05 05-02-2008 08:10 AM

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


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