LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-12-2009, 02:40 AM   #1
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Rep: Reputation: 17
Best way to save top output?


Hi guys. Im trying to capture top output but it doesnt seem to be an easy task:

- Saving top output with redirect sign (>) gives me binary file and very hard to read format.

- If I run top in batch mode (-b), it seems providing me an incorrect output. I let top run for 1 hour, and the cpu info is exactly the same. For 1 hour, the idle percentage is always 87.9% :S

So, what would be the best way to log top output? Thanks.

Last edited by 18Googol2; 03-12-2009 at 03:53 AM.
 
Old 03-12-2009, 03:30 AM   #2
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Batch mode should work. What command did you run exactly? You know you have to scroll down the log file to see each new output right?
 
Old 03-12-2009, 03:52 AM   #3
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Original Poster
Rep: Reputation: 17
Here is the full command I used, it is part of a while loop:

Code:
% top -bcn 1
And it is output from top:

Code:
% grep "% id" top_output.log
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.7% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.8% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.8% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.8% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.8% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.8% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
Cpu(s):  4.6% us,  3.8% sy,  0.0% ni, 87.9% id,  3.8% wa,  0.0% hi,  0.0% si
 
Old 03-12-2009, 04:39 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perhaps you'd better share (some more of) your script with us. I regularly use top in batch saving to a file in a timed loop for diagnostics.
Better would be something like sar of course.
 
Old 03-12-2009, 04:41 AM   #5
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Original Poster
Rep: Reputation: 17
It is very simple:

Code:
while true; do
        top -bcn 1 | head -30 >> top_output.log
        sleep 3
done
syg00, can you post your full top command and its output, please?

Last edited by 18Googol2; 03-12-2009 at 11:05 PM.
 
Old 03-12-2009, 06:18 AM   #6
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Even this works:
Code:
top -b > topout.log
Edit: didn't see it was the OP posting code above me. You need to take the 2nd pipe out of that command or it will not produce any output to the file. Maybe that's a typo as you said you were getting some output - but I just copied/pasted your code and it behaved as expected, with different output for each execution of top.

Last edited by openSauce; 03-12-2009 at 06:22 AM.
 
Old 03-12-2009, 11:08 PM   #7
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Original Poster
Rep: Reputation: 17
It was definitely a typo, and has been fixed.

I did get the different top output, except the CPU percentage.

Can you run that top command again and post your CPU % info? Thanks.
 
Old 03-13-2009, 12:16 AM   #8
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
It doesn't seem to change much.

I executed
Code:
for i in $(seq 1 5); do 
  top -bcn 1 | head -30 >> top_output.log;
  sleep 3;
done

$ grep Cpu top_output.log
twice. First time almost nothing changed, 2nd time I started some applications while it was going and it changed a little:
Code:
Cpu(s): 10.8%us,  2.6%sy,  0.8%ni, 73.8%id, 11.9%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 10.8%us,  2.6%sy,  0.8%ni, 73.8%id, 11.9%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 10.8%us,  2.6%sy,  0.8%ni, 73.8%id, 11.9%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 10.8%us,  2.6%sy,  0.8%ni, 73.8%id, 11.8%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 10.8%us,  2.6%sy,  0.8%ni, 73.9%id, 11.8%wa,  0.1%hi,  0.1%si,  0.0%st
Code:
Cpu(s): 11.7%us,  2.6%sy,  0.7%ni, 73.9%id, 10.9%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 11.7%us,  2.6%sy,  0.7%ni, 73.9%id, 10.9%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 11.7%us,  2.6%sy,  0.7%ni, 73.8%id, 11.0%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 11.8%us,  2.6%sy,  0.7%ni, 73.7%id, 10.9%wa,  0.1%hi,  0.1%si,  0.0%st
Cpu(s): 11.9%us,  2.6%sy,  0.7%ni, 73.7%id, 10.9%wa,  0.1%hi,  0.1%si,  0.0%st
Are you sure the no change is actually incorrect? Try putting a heavy load on your CPU and see what happens. I am a bit suspicious, viewing the output of top in normal mode I get bigger changes, going up to 20%us even when I'm not doing anything.

Last edited by openSauce; 03-13-2009 at 12:20 AM.
 
Old 03-13-2009, 12:47 AM   #9
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
top -bn5 > out

seems to produce more realistic results. Maybe the first iteration of top isn't reliable?
 
Old 03-13-2009, 01:22 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Hmmm - I thought I had already responded to this, but obviously not.
I presume top suffers the same "problem" as vmstat (see the manpage). On that basis, I only use "-n 1" when I am looking at the individual process data, not the summary data at the beginning of the display. Hence I use a decent iteration count, and loop based on a timer - say
Code:
for i in `seq 1 10` ; do top -b -n 8 -d 3 >> data.top ; sleep 300 ; done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Top 10 ways to save PC computing energy LXer Syndicated Linux News 0 09-13-2008 04:20 PM
Help me understand my top output neocontrol Linux - Server 5 02-21-2008 05:46 PM
Help Determinig Top Output XaViaR Linux - General 2 02-13-2007 04:41 PM
Output of 'top' as backgrond Burgin Linux - General 2 06-09-2005 05:18 PM
Odd output of 'top' philforrest Linux - General 1 07-20-2004 12:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration