Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Under a UNIX shell, how can I get a similar effect to the watch command, but with paging so that I can scroll around in the output if it takes up more than one screen?

In other words, I want a program that is to watch what less is to cat.

As an example, lets say I wanted to watch the output of qstat, I could use

watch qstat

to watch the output of qstat, but this can only shows the first screenful.

With a paging version of watch, I would be able to move around in the output as it is still continuously updated by watch. Is there any way to do this at the moment with existing utilities?

share|improve this question

migrated from stackoverflow.com Aug 6 '09 at 5:35

This question came from our site for professional and enthusiast programmers.

    
This probably belongs on SuperUser or maybe ServerFault. –  Dav Aug 6 '09 at 4:59
    
You are probably right, how can I move it? –  David Dean Aug 6 '09 at 5:14
    
Three people have voted to move it so far; if two more people (or a moderator) vote to move it, then it will happen automatically. –  Greg Hewgill Aug 6 '09 at 5:27
    
Why not just open a bigger window and go away with paging altogether? –  Marcin Aug 7 '09 at 0:22
1  
because my screen is already full? why use less when you can just use cat and a bigger window? –  David Dean Aug 7 '09 at 0:34

9 Answers 9

up vote 11 down vote accepted
+50

Rather than modifying the 'watch' command, use screen!

For example, let's say that you need to be able to see 300 lines of height and 100 characters of width and move around that. After starting screen, force the size thus:

<C-A>:height -w 300
<C-A>:width -w 100

Now start your watch command. You can then use <C-A>ESC to page around the display.

Unfortunately, the display doesn't refresh while in copy mode. But if you want to adjust which section of the window you're viewing, the easiest way may be to rerun the height/width commands as by default your terminal shows the lower-right of the virtual window.

share|improve this answer
    
the only issue then is how to continually repeat the command, while blanking the screen between each go –  David Dean Aug 18 '09 at 1:12
    
Oops, I meant run the watch command inside screen. Fixed. –  MikeyB Aug 18 '09 at 13:19
    
yartls - yet another reason to love screen :) –  warren Sep 7 '09 at 7:30

you can try:

watch command > file

then in your file you should see the appendend output (I don't have a linux box rigth now to test this)

share|improve this answer
1  
That isn't going to do what you intend, really, it will just fill up file with the same output over and over. It certainly doesn't address the original question. –  Greg Hewgill Aug 6 '09 at 4:55
    
then I dunno :) –  dfa Aug 6 '09 at 5:08
1  
Change the ">" to ">>" to make it append the data to to the file? –  Rory Aug 12 '09 at 10:32

OK, I've had a little go at a watchless function. It's a bit rough, and it doesn't yet appear to completely work, but here goes:

#!/bin/bash -u
out=$(mktemp)
(while [ 1 ]; do
    "$@" > $out;
    sleep 2;
done) &
less $out
kill $!

You have to manually use the R key in less to get the display to update.

It appears to work for watchless date but not for watchless qstat or watchless pstree, which both show blank. Any ideas?

share|improve this answer

You can try this:

while vmstat; do sleep 1; done | less

replace vmstat with qstat and adjust the sleep to your needs

share|improve this answer
    
all this does is keep repeating the command into less, which means that you need to keep scrolling to see the latest output. –  David Dean Aug 14 '09 at 6:32
    
Yes, but you can scroll back. You can't have both at once. Pressing shift f, that is capital 'F' will work like tail. –  Spacen Jasset Aug 14 '09 at 14:06

I don't see how this could be implemented as the row contents change, and watch would reset back to first line every 2 seconds even if you could scroll down.

Some workarounds are:

watch 'qstat | tail -n40' to show output of qstat beginning from 40th line from bottom

watch 'qstat | grep jsmith' to grep the output so the lines you are interested in are always in the first screen.

Note that you need to put the commands around the pipe in single quotes - otherwise you will be piping the output of watch, not the output of qstat.

share|improve this answer

To continue on enkrs's answer,

watch 'qstat | head -300 | tail -15'

will get you arbitrary pages into the qstat's output.

share|improve this answer

Here's a rather crude script that seems to work for several commands that I threw at it

#!/bin/bash
# ---- mywatch.sh ----

if [ $# -lt 1 && $# -gt 2 ]; then
    echo "Usage: $0 <command> <delay>" 
    exit 1
fi

CMD=$1
if [ $# -eq 2 ]; then
  DELAY=$2
else
  DELAY=2 # default
fi

while : ; do
  ( (echo -e "Every ${DELAY}s: $CMD\n"; $CMD) | less )&
  PID=$!
  sleep $DELAY
  kill -9 $PID &> /dev/null
  clear
done

Used as such:

alias mywatch="~/bin/mywatch.sh"

mywatch vmstat
mywatch "ps aux" # commands in options need to be quoted
mywaych pstree 10 # change delays

Being rather pedantic, the transition between refreshes aren't as smooth as I would like it to be. Naturally, being a simple script it doesn't support highlight of diff (watch -d). Also, the parsing of input arguments can be done better.

share|improve this answer

Multitail: http://www.vanheusden.com/multitail/

Example:

 vmstat 1 |multitail -j

Scroll back by press 'b' and page/arrow up/down.

share|improve this answer

How about just: tail -f

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.