Unix Shell - Need script to select and update data from 'TOP'

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2004 > Need script to select and update data from 'TOP'





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Need script to select and update data from 'TOP'
Gern Blandon

2004-05-12, 1:38 am

Hello:

Using either bash or tcsh, how can I run the TOP command and then
select a specific process ID and display the data from that process in
another terminal window?

More specifically, I'd like to monitor the CPU and memory usage of a
running app that I think is leaking without having to wade through all
the data displayed by TOP. So, I want to run the TOP command in one
terminal window and then, in another terminal window, selectively show
constantly updated data from the CPU and RPRVT columns for the
application I'm interested in. I know this can be done because I've
seen it done before but damn if I can remember how it's done. I'm
especially puzzled at the command required to display data from one
terminal window in another. I know this script probably involves using
TOP, |, and SED or GREP to search for the process ID and column names
I'm interested, but displaying this data in another terminal window has
got me stumped.

Any help would be most appreciated.
Thanks,

GB

Alan Connor

2004-05-12, 2:35 am

On Wed, 12 May 2004 05:09:49 GMT, Gern Blandon <jv44@mindspring.com> wrote:
>
>
> Hello:
>
> Using either bash or tcsh, how can I run the TOP command and then
> select a specific process ID and display the data from that process in
> another terminal window?
>
> More specifically, I'd like to monitor the CPU and memory usage of a
> running app that I think is leaking without having to wade through all
> the data displayed by TOP. So, I want to run the TOP command in one
> terminal window and then, in another terminal window, selectively show
> constantly updated data from the CPU and RPRVT columns for the
> application I'm interested in. I know this can be done because I've
> seen it done before but damn if I can remember how it's done. I'm
> especially puzzled at the command required to display data from one
> terminal window in another. I know this script probably involves using
> TOP, |, and SED or GREP to search for the process ID and column names
> I'm interested, but displaying this data in another terminal window has
> got me stumped.
>
> Any help would be most appreciated.
> Thanks,
>
> GB
>


Here's how I'd do it, I think:
#
# #!/bin/sh
#
# echo >> ~/toplog
# date >> ~/toplog
# echo >> ~/toplog
# echo " PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND" >> ~/toplog
# echo >> ~/toplog
#
# while true ; do clear
# echo; echo
# echo " PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND"
# echo
# top b n 1 | grep 'string' | tee -a ~/toplog
# echo; echo
# sleep 10
#
# done
#
# # Ctrl-c to exit
#
This will give you a running log in your current screen, updated every 10 seconds,
and log the results to ~/toplog with starting time/date of the current session.

Should work :-)

HTH

AC

Kevin Collins

2004-05-12, 2:57 pm

In article <2004051122094842238%jv44@mindspringcom>, Gern Blandon wrote:
> Hello:
>
> Using either bash or tcsh, how can I run the TOP command and then
> select a specific process ID and display the data from that process in
> another terminal window?
>
> More specifically, I'd like to monitor the CPU and memory usage of a
> running app that I think is leaking without having to wade through all
> the data displayed by TOP. So, I want to run the TOP command in one
> terminal window and then, in another terminal window, selectively show
> constantly updated data from the CPU and RPRVT columns for the
> application I'm interested in. I know this can be done because I've
> seen it done before but damn if I can remember how it's done. I'm
> especially puzzled at the command required to display data from one
> terminal window in another. I know this script probably involves using
> TOP, |, and SED or GREP to search for the process ID and column names
> I'm interested, but displaying this data in another terminal window has
> got me stumped.
>


Not sure, but I don't know any commands called TOP, SED or GREP. Maybe you mean
top, sed and grep?

Kevin
joe@invalid.address

2004-05-12, 2:57 pm

Gern Blandon <jv44@mindspring.com> writes:

> Hello:
>
> Using either bash or tcsh, how can I run the TOP command and then
> select a specific process ID and display the data from that process in
> another terminal window?


Find the process you're interested in, then use the -p option of top
in another window.

Joe
--
"Surprise me"
- Yogi Berra when asked where he wanted to be buried.
joe@invalid.address

2004-05-12, 2:58 pm

Gern Blandon <jv44@mindspring.com> writes:

> Using either bash or tcsh, how can I run the TOP command and then
> select a specific process ID and display the data from that process in
> another terminal window?


Depends on the system you're on. Some systems have a -p option which
you can use to display only selected processes. If you're on Solaris 8
or later look at prstat.

Joe
--
"Surprise me"
- Yogi Berra when asked where he wanted to be buried.
Chris F.A. Johnson

2004-05-12, 4:59 pm

On 2004-05-12, Gern Blandon wrote:
> Hello:
>
> Using either bash or tcsh, how can I run the TOP command and then
> select a specific process ID and display the data from that process in
> another terminal window?
>
> More specifically, I'd like to monitor the CPU and memory usage of a
> running app that I think is leaking without having to wade through all
> the data displayed by TOP. So, I want to run the TOP command in one
> terminal window and then, in another terminal window, selectively show
> constantly updated data from the CPU and RPRVT columns for the
> application I'm interested in. I know this can be done because I've
> seen it done before but damn if I can remember how it's done. I'm
> especially puzzled at the command required to display data from one
> terminal window in another. I know this script probably involves using
> TOP, |, and SED or GREP to search for the process ID and column names
> I'm interested, but displaying this data in another terminal window has
> got me stumped.
>
> Any help would be most appreciated.


Why not use "ps PID" instead of top? Then you don't have to wade
through a lot of unnecessary data.

Modern versions of ps allow the user to format its output, so you
don't get extraneous information.

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Gern Blandon

2004-05-17, 8:35 pm

Thanks for this, Alan. I'll give it a try. And thanks to everyone else
for the feedback. It's been very helpful.

GB

On 2004-05-11 23:03:39 -0700, Alan Connor <zzzzzz@xxx.yyy> said:

> On Wed, 12 May 2004 05:09:49 GMT, Gern Blandon <jv44@mindspring.com> wrote:
>
> Here's how I'd do it, I think:
> # # #!/bin/sh
> # # echo >> ~/toplog
> # date >> ~/toplog
> # echo >> ~/toplog
> # echo " PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM
> TIME COMMAND" >> ~/toplog
> # echo >> ~/toplog
> # # while true ; do clear
> # echo; echo
> # echo " PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM
> TIME COMMAND"
> # echo
> # top b n 1 | grep 'string' | tee -a ~/toplog
> # echo; echo
> # sleep 10
> # # done
> # # # Ctrl-c to exit
> # This will give you a running log in your current screen, updated
> every 10 seconds,
> and log the results to ~/toplog with starting time/date of the current session.
>
> Should work :-)
>
> HTH
>
> AC



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com