Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to monitor one process's memory / cpu usage in real time. Similar to top but targeted at only one process, preferably with a history graph of some sort.

share|improve this question
    
What memory statistics do you want? There are lots of them. –  vwduder Jan 13 '11 at 11:33
    
Memory usage over a given time frame, current usage, maximum usage, average. –  Josh K Jan 13 '11 at 11:33
add comment

4 Answers

up vote 21 down vote accepted

On Linux, top actually supports focusing on a single process, although it naturally doesn't have a history graph:

top -p PID

This is also available on Mac OS X with a different syntax:

top -pid PID
share|improve this answer
3  
And since you may not want to look up the PID every time, try something like top -p `pgrep -f /usr/bin/kvm`. –  Stefan Lasiewski Aug 17 '10 at 3:33
    
I use Cacti to monitor some individual processes, but installing a full blown Cacti installation sounds too complex for the simple situation asked here. –  Stefan Lasiewski Aug 17 '10 at 3:34
    
@Stefan: I'm assuming I would have to run that remotely? –  Josh K Aug 17 '10 at 4:00
    
@Josh : Yes you would need to run Cacti (Which requires MySQL, Apache and few other packages) on another server. On most distros, it's pretty simple to install using Yum or apt-get. –  Stefan Lasiewski Aug 17 '10 at 20:48
add comment

I normally use following two :

  1. HP caliper : its very good tool for monitoring processes it you can check call graph and other low level information also. But please note its free only for personal use.

  2. daemontools : a collection of tools for managing UNIX services

share|improve this answer
1  
I used daemontools for years. It's great as a supervisor/watchdog for other processes. How does it help you monitor CPU/memory usage for one process? –  Stefan Lasiewski Aug 19 '10 at 4:05
add comment

htop is a great replacement to top. It has... Colors! Simple keyboard shortcuts! Scroll the list using the arrow keys! Kill a process without leaving and without taking note of the PID! Mark multiple processes and kill them all!

Among all of the features, the manpage says you can press F to follow a process.

Really, you should try htop. I never started top again, after the first time I used htop.

share|improve this answer
1  
+1 for htop. This is one of the first program I install on a new system. It makes my life much easier. The tree view is also very handy. –  Barthelemy Nov 24 '10 at 12:22
    
top also has colors. Press z. –  Tshepang Jan 12 '11 at 1:41
1  
You're right! top has colors! Too bad its colors are quite useless, specially when compared to htop (which fades other users processes and highlights the program basename). –  Denilson Sá Jan 12 '11 at 18:17
    
top can also kill processes without leaving. Just hit k. –  AvatarOfChronos Jul 19 '12 at 13:57
add comment

To use that information on a script you can do this:

calcPercCpu.sh

#!/bin/bash
nPid=$1;
nTimes=10; # customize it
delay=0.1; # customize it
strCalc=`top -d $delay -b -n $nTimes -p $nPid \
  |grep $nPid \
  |sed -r -e "s;\s\s*; ;g" -e "s;^ *;;" \
  |cut -d' ' -f9 \
  |tr '\n' '+' \
  |sed -r -e "s;(.*)[+]$;\1;" -e "s/.*/scale=2;(&)\/$nTimes/"`;
nPercCpu=`echo "$strCalc" |bc -l`
echo $nPercCpu

use like: calcPercCpu.sh 1234 where 1234 is the pid

For the specified $nPid, it will measure the average of 10 snapshots of the cpu usage in a whole of 1 second (delay of 0.1s each * nTimes=10); that provides a good and fast accurate result of what is happening in the very moment.

Tweak the variables to your needs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.