Piping output of "top" to a text file

I would like to pipe "top -n" to a text file, but I get an error:

top: cannot open /dev/kmem
kvm_open: Permission denied

I am a non-root user.

If I could find a way to get this type of output:
"Memory: 2048M real, 1516M free, 4099M swap free"
into a text file, I could further automate my daily system healthchecks.

Any ideas would be appreciated. Thanks so much.

See this post and check your man page for top.

Thanks, RTM, for your quick reply. I did search the forum before posting, but did not find the post you reference. I did also try man and google and the Sun site, but could not find the help I needed. (Just so you know, I am not a lazy newbie, just a clueless one!)

I do not have man pages for top on any of my five Solaris servers. This is probably because I rigged top to run by myself, as I couldn't get a unix admin who had time to do it. I copied an executable from somewhere else & got it to run. But I did not so an actual install.

These are the options I have for top on my machines:
Top version 3.5beta8
Usage: top-3.5b8-sun4u-5.9-64bit [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]

I tried the "-f" option, even though it is not listed, but no success.

Any other ideas you have for me will be appreciated. Thanks so much.

How about:

$ swap -s
total: 52056k bytes allocated + 28144k reserved = 80200k used, 1321528k available

Carl

You could also try this:

$ vmstat 1 2 | tail -1
 0 0 0 1322760 331512 0   7  0  0  0  0  0  0  0  0  0  324  112  154  1  0 99

Position 4 ad 5 are swap and free.

Carl

Carl,

Thanks so much for your replies.

These will only give me virtual memory, is that correct? Are there similar commands for physical memory usage? I have used vmstat before, but swap is new to me; I'm glad to learn of it.

I am a little embarrassed to be asking such elementary questions. Thanks again.

Shirley

prtconf -v | grep -i mem

>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!

doing 'man vmstat' yields:

     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'

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).

$ 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

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

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

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.

My results:

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.

My Results:

 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).

My results:

 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.

My Results:

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.

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:

cpu % 24.3

This was the command that satisfied my problem:
replace 12345 with your PID:

/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}' 

:confused::confused::rolleyes::):smiley: