Piping output of "top" to a text file

 
Thread Tools Search this Thread
Operating Systems Linux Fedora Piping output of "top" to a text file
# 8  
Old 09-20-2006
>prtconf -v | grep -i mem
Memory size: 2048 Megabytes
memory (driver not attached)
virtual-memory (driver not attached)
memory-controller, instance #0
memory-controller, instance #1

This seems to return the amount of physical memory I have, but not what is being used.

We are getting closer!
# 9  
Old 09-20-2006
doing 'man vmstat' yields:
Code:
     memory
           Report on usage of virtual and real memory.

           swap  amount  of  swap   space   currently   available
                 (Kbytes)

           free  size of the free list (Kbytes)

used = 'Memory size: 2048 Megabytes' - 'free size of the free list (Kbytes)'

I'm not sure though - others might correct the 'math'
# 10  
Old 09-20-2006
Since your question is performance related, you really don't need to know how much physical memory is in use. vmstat will give you the performance information you're looking for (I cleaned up the output a little, format wise).

Code:
$ vmstat 5
 procs     memory            page            disk               faults      cpu
 r b w   swap     free   re  mf pi po fr de sr s6 s8 s8 s8   in   sy   cs us sy id
 0 0 0 14334608 10300056 25 146 13  1  1  0  0  0  0  0  0  708  172 4666  2  2 96
 0 0 0 14221840  9149336  0   2  0  0  0  0  0  0  0  0  0  622 6071 3948  0  1 99
 0 0 0 14221864  9149360  0   1  0  0  0  0  0  0  1  0  0  808 6476 4026  0  1 98
 0 0 0 14221880  9149376  0   0  0  0  0  0  0  0  0  0  0  647 6157 3998  0  1 98

Distilled from Sun Performance and Tuning by Adrian Cockcroft:

The sr column (Scan Rate) is probably the most important when determining whether you're short on memory or not. When this number gets to a high rate (200 pages per second averaged over 30 seconds), then you're short on memory and need to add more.

Swap space and available swap isn't a real help since the system will balance things out and the free column will stablize out.

For CPU's, four times the number of CPUs in the run queue (the procs/r column) means you should add another cpu.

For disk slowness, the blocked queue gives you the info (procs/b column). If b => r then you should look at balancing your data or perhaps getting a RAID in place.

Carl
# 11  
Old 09-20-2006
Did you lookat pmap
pmap -x <PID>
# 12  
Old 09-20-2006
Quote:
Originally Posted by sssow
Did you lookat pmap
pmap -x <PID>
I think that's fine if you're troubleshooting a memory issue, but it sounds like from his initial request, that he wanted something to keep an eye on. "Healthchecking" he said. I think the vmstat info would give good health checking information.

Carl
# 13  
Old 09-21-2006
I am very appreciative that you are all helping me with this. Let me first explain what I am trying to accomplish, and then let me ask for clarification about vmstat and top.

I am a middleware sys admin (not a Unix admin). I do a daily healthcheck of the Solaris servers where my software (MQSeries) runs. In addition to looking at specific MQ logs and queues, I also look at some system-level stats, including CPU and memory usage. This is not a "scientific" look; rather, it's a point-in-time peek that has allowed me to develop a "baseline" of normal operations. I look at the system-level stuff because 1) these are dedicated middleware servers & nothing else runs on them and 2) MQ & related software spawns many processes (that's why I don't just look at one process).

This approach has helped me catch problems several times.

My specific question about vmstat and top is about interpreting the data. (Remember, I'm not a unix admin!)

Here is vmstat output:
r b w swap free re mf pi po fr de sr m1 m1 m1 m2 in sy cs us sy id
0 0 0 5622568 1636016 38 137 5 0 0 0 0 0 0 0 0 258 1235 307 1 1 99

I *think* this means that I have 5.6 Gb of total memory available, of which 1.6 Gb is free.

Here is the top output:
Memory: 2048M real, 1555M free, 4099M swap free

I *think* this means that I have 2.0 GB of physical memory, of which 1.5 Gb is free AND I have 4.1 GB of swap that is also free.

I must not understand how to read these outputs, eh?

Gratefully yours,
Shirley
# 14  
Old 05-21-2009
pipe the cleansed output of top to command line:

I have a Fedora Core 8 box I ran these on, it should work for Unix and Linux systems:

This command pipes the cleansed output of 'top' to command line and cleans the escape codes.

Quote:
top -cSb n 1
My results:
Code:
top - 15:26:24 up  7:10,  3 users,  load average: 0.18, 0.23, 0.28
Tasks: 223 total,   2 running, 220 sleeping,   0 stopped,   1 zombie
Cpu(s):  7.2%us,  1.8%sy,  0.4%ni, 89.6%id,  0.9%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   6219640k total,  5871364k used,   348276k free,   190064k buffers
Swap:  2031608k total,        0k used,  2031608k free,  3871044k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3812 el        20   0  364m 130m  31m S   10  2.2   7:28.10 /usr/lib/firefox-3.0/firefox-bin
 2323 java    20   0 2061m  20m  18m S    4  0.3   0:06.92 ora_ckpt_kurdev
 6277 el        20   0 35116 4196 3372 S    4  0.1   1:20.36 /usr/bin/pulseaudio -D
...
...

Use this to pipe the cleansed output of 'top' to command line and cut off the unnecessary header lines.

Quote:
top -cSb n 1 | tail -n+8
My Results:
Code:
 3812 el        20   0  364m 130m  31m S    7  2.1   8:32.04 /usr/lib/firefox-3.0/firefox-bin
 6277 el        20   0 35116 4196 3372 S    7  0.1   1:34.07 /usr/bin/pulseaudio -D
 6051 root      20   0  379m  34m  21m S    2  0.6  12:15.65 /usr/bin/X -br -nolisten tcp :0 vt7 -auth /var/run/xauth/A:0-7jJzWo
21044 el        20   0  2532  996  712 R    2  0.0   0:00.02 top -cSb n 1
...
...

How to get the top output for only one pid (process id).

Quote:
top -cSb n 1 -p 3812 | tail -n+8
My results:
Code:
 3812 el        20   0  364m 130m  31m S   10  2.2   9:01.30 /usr/lib/firefox-3.0/firefox-bin

How to track the cpu usage for a single process in Linux/Unix:
*Important: change the process id '3812' to one of your PID's.

Quote:
top -cSb -p 3812 n 1 | tail -n+8 | sort -rn -k5 | awk 'NR > 0 { s +=$9 }; END {print "cpu %",s}'
My Results:
Code:
cpu % 8

For some reason the 'ps' command does not come out with the same results as top does (based on my observation). Top yields the most accurate summary of how much CPU your process is using.

You could also use ps to figure out how much total CPU load in Linux/Unix.

Quote:
ps aux|awk 'NR > 0 { s +=$3 }; END {print "cpu %",s}'
I make no guarantees for this command, it seems to match up with top's version, however if you have a dual core, or more than one core, this number returned will go to 200%. It's a hack.

My Results:

Code:
cpu % 24.3

This was the command that satisfied my problem:
replace 12345 with your PID:
Code:
/usr/bin/top -cSb -p 12345 n 1 | /usr/bin/tail -n+8 | /bin/sort -rn -k5 | /usr/bin/awk 'NR > 0 { s +=$9 }; END {print s}'

SmilieSmilieSmilieSmilieSmilie

Last edited by sentientmachine; 05-21-2009 at 06:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Homework & Coursework Questions

Need help creating shell script with output that has 2014 calendar and 2 text items from a"fortune"

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am required to create a bash shell script with either emacs or vi. It must include the year 2014 calendar on... (9 Replies)
Discussion started by: dandanhelpmeman
9 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Retrieve RAM memory size from "top" command output

Hi, I am trying to get the system RAM size from "top" command's output by the following but it is not working. top | sed "s/^Mem.**\(*\), *//" (10 Replies)
Discussion started by: royalibrahim
10 Replies

6. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

7. AIX

AIX 5.3 - Discrepancies between "top" and "vmstat"

Can someone explain the differences I'm seeing below in TOP and VMSTAT commands on my AIX 5.3 server? Thanks! CPUs: 4; load averages: 0.86, 0.97, 0.97 18:09:26 926 processes: 4 stopped, 922 running CPU states: 78.4% idle, 8.5% user, 12.6% kernel, 0.3% wait Memory: 23680M Total.... (1 Reply)
Discussion started by: troym72
1 Replies

8. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies

9. UNIX for Dummies Questions & Answers

Process seen in "ps aux" but not "top"

Hi, I have a process that can be seen after "ps aux" command. However when I do "top" command. This process cannot be seen. How can this happen? Is there anything wrong with my code? (3 Replies)
Discussion started by: monkfan
3 Replies

10. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies
Login or Register to Ask a Question