I would like to monitor specific processes via TOP on our application servers via TOP. Currently, I have been using the following in BASH to monitor just the processes that I am interested in:

top -p $(pgrep PSAPPSRV | tr "\\n" "," | sed 's/,$//')

I had thought that this was a great solution, but there are two caveats I was overlooking:

  1. A single process can recycle itself, creating a new PID
  2. When the existing processes are under load, the server can spawn new processes to cope with the load.

Both situations mean that I can't monitor the processes for an extended amount of time if I use a 'snapshot' of the PIDs at point-in-time that I run the command above.

These processes run as a dedicated user. However, this user runs all of the processes associated with the application server. And, an application server may actually consist of 30 processes, but I'm only interested in a specific process.

Is it possible to pass new PIDs into TOP output that is already running?

Or would the best strategy be to re-call TOP at certain intervals, passing in the PIDs at that current time?

  • 1
    Is there any reason you can't run these processes under a dedicated user and use the -U option to top to only monitor that user's processes? – voretaq7 Jun 11 '13 at 18:25
  • @voretaq7 Thanks for that reminder. I forgot to include that in my original post. Post edited. – gdieckmann Jun 11 '13 at 18:32
  • For the future it's better to fold your edits into the body of your post (you should present a single clear question with all the relevant information right up front - edit blocks just make it harder to read) - I did a basic cut and fold here, feel free to clean it up further :) – voretaq7 Jun 11 '13 at 18:53

top really isn't designed to do what you're trying to do (in fact some versions of top don't even support the -p option).

The best option I can think of for your situation would be to wrap top in a script that calls it repeatedly with the -d and -n options, e.g.

while [ true ]; do
    top -d1 -n15 -p $(pgrep PSAPPSRV | tr "\\n" "," | sed 's/,$//')
    sleep 1

which will make top show one screen with up to 15 processes, sleep for one second, then do it again (until you abort the script with CTRL+C.

This loses top's advanced functionality (like the ability to change sort order - so make sure you specify that if it's important), but will give you the same sort of display without much work.

  • Thanks! Now I'm curious about whether I'm using the correct tool or not. My understanding is that TOP gives the CPU usage for a single process as an estimation of the current CPU usage. I started exploring ps instead of top, but noticed that ps gives the CPU usage over the lifetime of a process. I'm more interested in the CPU usage at a point in time, which I can use to approximate whether or not the application server would spawn a new process. Is there a tool that I'm overlooking that will show current CPU usage? Also, let me know if I got too off-topic. – gdieckmann Jun 11 '13 at 20:52
  • top sounds like the right tool for what you want (ps will give you weighted CPU usage for a process too with the right options - you don't want "CPU Time", you want "%cpu" or "pcpu" -- it won't show the system total though). top is just not adapted to tracking processes by name - it's more a "Show me the processes currently running ordered by <resource>" tool than a "Show me what this specific process is using" tool. You can bend it to your will with a little effort though. – voretaq7 Jun 11 '13 at 20:57
  • Okay, I just wanted to make sure that I wasn't missing a tool that would be a better fit than either top or ps. Thanks for all of the explanations! – gdieckmann Jun 11 '13 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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