Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132

    Question top -n 1 without "clearing screen"


    Is there a way to run "top -n 1|head -n 15|tail -n 10" combo without it seemingly "clearing the screen", which it is not really doing a good job, when the terminal has not 25 rows; I also use head and tail to just get the lines I want.

    To show you what top did I "cleared the screen" not by "clear", but via pressing multiple times "Return".

    So, the first command I gave to the terminal is on the very last line, which produced the output now found on lines 7 - 15, and after that the other commands I submitted after the top/head/tail one..
    Code:
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
     2028 rava      20   0 2630m 1.7g  31m S   20 52.7 246:38.22 palemoon           
     3213 rava      20   0 1469m 404m  10m S    5 11.9 109:33.77 plugin-containe    
        7 root      20   0     0    0    0 R    2  0.0   0:09.41 rcu_preempt        
     1531 root      20   0  330m  97m  27m S    2  2.9  60:14.14 X                  
     9795 rava      20   0 19616 2496 2068 R    2  0.1   0:00.02 top                
        1 root      20   0   240    0    0 S    0  0.0   0:01.27 init               
        2 root      20   0     0    0    0 S    0  0.0   0:00.00 kthreadd           
        3 root      20   0     0    0    0 S    0  0.0   0:05.51 ksoftirqd/0        
    rava@porteus:/mnt$ echo $COLUMNS 
    80_________________
    rava@porteus:/mnt$ tput lines
    34_________________ 
    rava@porteus:/mnt$ tput cols
    80_________________ 
    rava@porteus:/mnt$ top -version
    _________top: procps version 3.2.8
    usage:ortop -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pi
    d [,pid ...]__________
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ 
    rava@porteus:/mnt$ top -n 1|head -n 15|tail -n 10
    I looked into man top, but found no command that could tell top not to try and clearing of the screen, or more precisely, no moving up rows and not clearing the screen, which top instead did here.

    To make it better readable for you, I removed the "background" that was there due to my "Returns" and put in "____" instead.. So, in reality the output is way more messed up than what you see above, with output lines like so:
    Code:
    80va@porteus:/mnt$ 
    rava@portop: procps version 3.2.8
    usage:ortop -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...]orteus:/mnt$
    And no, "clear" prior the top command is not an option, since I want the older output to remain on screen, e.g. to be able to run several of the above top/head/tail commands and still be able to see the older output for comparison.

    Would a good solution be the use of a named pipe?

  2. #2
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,472
    Hi.

    One thing that comes to mind for managing display areas is GNU screen:
    Code:
           When screen is called, it creates a single window with a shell in it
           (or the specified command) and then gets out of your way so that you
           can use the program as you normally would. 
    ...
           split
    
           Split the current region into two new ones. All regions on the display
           are resized to make room for the new region. The blank window is
           displayed on the new region. Use the "remove" or the "only" command to
           delete regions.  Use "focus" to toggle between regions.
    See GNU Screen splitting - Tom Lee for a demonstration

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux Guru
    Join Date
    Dec 2013
    Posts
    2,747
    top is an interactive program, and you are hacking it apart with your heads and tails.
    no wonder your terminal is all messed up...
    fortunately top offers a command line option exactly for that situation:
    Quote Originally Posted by man top
    -b :Batch-mode operation
    Starts top in Batch mode, which could be useful for sending
    output from top to other programs or to a file. In this
    mode, top will not accept input and runs until the iterations
    limit you've set with the `-n' command-line option or until
    killed.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    739
    When you clear the screen, what it is displaying doesn't go away, it just scrolls up so you don't see it. You can scroll up and see it.

    You can also open a second terminal, or depending on the terminal, another tab in the same terminal, and run things in both independently, and switch back and forth as you like.

    But I think the batch mode offered by nihili is probably what you're looking for.

  6. #5
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132
    -->
    top -bn 1|head -n 16|tail -n 10 indeed is what does the trick, thanks for the head up!
    Cheers!
    yours truly, Rava

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •