LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Using top in batch mode (https://www.linuxquestions.org/questions/linux-server-73/using-top-in-batch-mode-559638/)

neothephoenix 06-06-2007 10:41 AM

Using top in batch mode
 
Hi,

I need to redirect the output of top command to a text file for monitoring the CPU utilization later on. I am using RHEL 3 AS (2.4.21-32.EL).
I used the following command:

#top -b -n 1>top.txt

but the command does not exit by itself and the text file size keeps on increasing until killed manually using Ctrl-D.

Please help!!!!

P.S. Is there a way by which I can take only the first 10-15 lines of top's output and discard the rest?

Vookimedlo 06-06-2007 01:05 PM

Hi,

it works as expected.

Code:

top -b -n1 >top.txt
'top' command goes through one iteration, then exits.
Using 'top' - version 3.2.7


To use only first 15 lines, try 'head' command.

DotHQ 06-06-2007 01:17 PM

Other tools for collecting historical data are sar and uptime.

Sar is set to run in 10 minute intervals by default (RH). To see if sar is running simply type:
sar
this will show data for the previous day only. Lots of other options though, as sar is very powerful. man sar for more info.

Load Average is a very useful tool for telling how busy a server really is. ie: if the cpu is at 90% but your load average is only 1.3 0.4 0.08 then you have nothing to worry about.
Load average by default is showing you the load average for the past: 1 minute, 10 minutes & 15 minutes. I will run uptime via cron every 10 minutes just to monitor the load average on a machine. ie:
/usr/bin/uptime >> uptime.out060607
Just redirect the output to a file, as above. Then, if desired you could email the file yourself at midnight, and zero out the file for the next day of stats.
A general rule of thumb is any load average under 2 is fine. Load average equates to a line at a movie theatre. If more than 2 people are waiting in line, service is not so fast. By the same token a load average of 2 means two processes are waiting for service by the CPU. Anything over that, if sustained, your response time will be affected.

syg00 06-06-2007 01:41 PM

"sar" is a good tool, but sometimes it is necessary to get the detail "top" provides. Running in batch mode should work fine.

@DotHQ: sar will give you the load averages, so you are collecting this twice.
Quote:

By the same token a load average of 2 means two processes are waiting for service by the CPU
Oft quoted but not necessarily true.
The load average also includes tasks in "uninterruptible sleep". Generally this means (disk) I/O. Can be the major contributor to the loadavg numbers, especially if the runqueue is short.

DotHQ 06-07-2007 09:29 AM

Quote:

Originally Posted by syg00
Oft quoted but not necessarily true.
The load average also includes tasks in "uninterruptible sleep". Generally this means (disk) I/O. Can be the major contributor to the loadavg numbers, especially if the runqueue is short.

I do believe that is why it's called a 'Rule of thumb'. It is more true than untrue in my opinion.

neothephoenix 06-09-2007 02:21 PM

Thank you!! One and all. The info provided was really helpful.:)


All times are GMT -5. The time now is 10:31 AM.