The standard watch
command is incredibly useful for monitoring command output in real-time, but it falls short when dealing with output that exceeds your terminal height. Unlike less
which allows scrolling through cat
's output, watch
simply truncates the display after the visible screen area.
While there isn't a direct "watch with paging" command, we can achieve similar functionality by combining existing tools:
watch -n 1 "command | less -R"
The -R
flag preserves ANSI color codes. However, this approach has limitations - you'll need to manually refresh by pressing Ctrl+L
in less
.
For a more robust solution, consider using terminal multiplexers:
tmux new-session 'while true; do command; sleep 1; done'
Then you can scroll freely in the tmux buffer while the command continues to update.
For more control, create a script like this:
#!/bin/bash
while true; do
clear
$@ | less -X -K
sleep 2
done
Save it as watchless
, make it executable, and use:
./watchless qstat -f
Some specialized tools can help:
multitail
- For monitoring multiple files/commandsdstat
- For system monitoring with paginghtop
- Interactive process viewer
For your qstat example, this works well:
tmux new-session 'while true; do qstat -f; sleep 5; done'
Now you can scroll through the full output while it updates every 5 seconds.
While the Unix watch
command is excellent for monitoring command output in real-time, it falls short when dealing with lengthy outputs that exceed terminal height. The standard implementation simply clears and refreshes the entire screen every 2 seconds (by default), making it impossible to scroll through historical data.
Here are several approaches to achieve paged, scrollable output while maintaining real-time updates:
1. Using tmux with watch
This method leverages terminal multiplexers to preserve scrollback:
tmux new-session 'watch -n 1 "qstat | less +F"'
2. Combining watch with less
A more direct approach using pipes:
watch -n 1 'qstat' | less +F
Note: This won't preserve color output. For colored output:
watch --color -n 1 'qstat --color=always' | less -R +F
3. Custom Script Solution
Create a bash function for reusable paged watching:
function pwatch() {
local interval=${1:-1}
shift
while true; do
clear
"$@" | less -X +F
sleep $interval
done
}
# Usage:
pwatch 1 qstat -f
For more sophisticated monitoring needs, consider this Python script that implements proper paging:
#!/usr/bin/env python3
import subprocess
import curses
import time
def main(stdscr, cmd, interval):
while True:
stdscr.clear()
try:
output = subprocess.check_output(cmd, shell=True).decode()
except subprocess.CalledProcessError as e:
output = f"Command failed: {e}"
stdscr.addstr(output)
stdscr.refresh()
time.sleep(interval)
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} 'command' [interval=1]")
sys.exit(1)
cmd = sys.argv[1]
interval = float(sys.argv[2]) if len(sys.argv) > 2 else 1.0
curses.wrapper(main, cmd, interval)
When implementing paged watch solutions, be mindful of:
- Command execution frequency impact on system load
- Terminal rendering performance with large outputs
- Memory usage for maintaining scrollback buffers
For specialized use cases, consider:
glances
- system monitoring with paging supporthtop
- interactive process viewermultitail
- multi-window tail with paging