LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2010, 06:25 PM   #1
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Rep: Reputation: 17
only the top - the 4 first lines


hi guys

I need to display some information like

CPU uptime
memory utilization
# user
and so on

and I see top has all that information I need I mean
Code:
top - 16:15:41 up 186 days,  5:43,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 117 total,   1 running, 116 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.4%sy,  0.0%ni, 99.2%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2075500k total,  1742620k used,   332880k free,   340148k buffers
Swap:  3047416k total,       64k used,  3047352k free,  1140408k cached
so is there a way to display the first 4 lines of top but I don't to miss the feature of continue being updates every 2 seconds

is there a way to filter those 4 lines and still getting that 4 lines updated?

thanks a lot
 
Old 04-08-2010, 06:31 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You'll almost certainly want to wrap "top" in a script to accomplish what you want.

Look at "man top" (the on-line help) and explore some of the options. For example, you might want to run "top -b" (batch mode) and run it through a bash, Perl or Python script. Or perhaps " top -n 1 | head -4" (number of iterations, piped through "head") in a bash loop.

The possibilities are endless - just experiment!
 
Old 04-08-2010, 06:33 PM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
In /bin/sh run:

Code:
while [ 1 ] ; do top | head -4 ; sleep 2 ; done
HTH

Forrest
 
Old 04-08-2010, 06:42 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Or
Code:
watch -n 2 'top -b -n1| head -n 4'
 
Old 04-08-2010, 06:48 PM   #5
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by forrestt View Post
In /bin/sh run:

Code:
while [ 1 ] ; do top | head -4 ; sleep 2 ; done
HTH

Forrest
nice trick

by the way is there a way to still quit using q?

is stays there is loop and only pressing Control+C Stops

thanks a lot
 
Old 04-08-2010, 06:58 PM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
You could change the while to look for a variable being equal to q and then change your sleep 2 to read w/ a timeout.

That would give you:

Code:
VAR=x ; while [ "${VAR}" != 'q' ] ; do top | head -4 ; read -sn 1 -t 2 VAR ; done
HTH

Forrest
 
Old 04-08-2010, 06:59 PM   #7
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
Tinkster, forrestt

really thanks I know is too much to say but just to mention it really does not update as the top command

some values for Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, changes continuously running top but using your great lines does not the same thing

any idea?
 
Old 04-08-2010, 07:03 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
That is an interesting observation ... I'm wondering whether top
(in batch mode) is actually broken ...

Simply doing a
top -b -n 1
and scrolling up on my machine gives the same values for CPU and such
every time ....
 
Old 04-08-2010, 07:10 PM   #9
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
watch does the job a little much better but both does not update CPU at all

you try running both commands

it has the quit stuff

Code:
VAR=x ; while [ "${VAR}" != 'q' ] ; do top | head -4 ; read -sn 1 -t 2 VAR ; done

don't have quit stuff yet
Code:
watch -n 2 'top -b -n1| head -n 4'
 
Old 04-09-2010, 11:05 AM   #10
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
nobody else?

thanks
 
Old 04-09-2010, 11:24 AM   #11
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
It looks like this is a bug in top as reported here.

HTH

Forrest
 
Old 04-09-2010, 11:40 AM   #12
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by forrestt View Post
It looks like this is a bug in top as reported here.

HTH

Forrest

wow since 2005 and has not been fixed....
 
Old 04-09-2010, 11:48 AM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
A quick skim of the comments on that bug report suggests that the fix is not all that simple.

Where does this issue rank in all the problems you face on a regular basis?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
top ten Simpsons gags/lines newbiesforever General 14 04-23-2009 07:48 AM
How to Delete nnn lines from the top of a file with out opening it? rhelpm Linux - Newbie 6 12-07-2006 03:46 PM
'N' lines of Top Command? DarkPenguin Linux - General 2 04-11-2006 12:05 PM
Getting multi-colour lines from top to bottom djeanveau Fedora - Installation 3 01-30-2005 09:58 PM
Getting multi-colour lines from top to bottom djeanveau Linux - Newbie 1 01-30-2005 01:02 PM


All times are GMT -5. The time now is 01:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration