Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

watch

Execute a program periodically, showing output fullscreen.

Description

Runs a command repeatedly at a configurable interval and displays its output in a fullscreen terminal view. Useful for monitoring changing system state. By default, the command is passed to sh -c for execution. The display is cleared and redrawn on each iteration, with an optional header showing the interval, command, and current time.

Inputs

  • External command execution via sh -c (default) or direct exec (with -x)
  • Terminal size via tcgetwinsize() syscall (rustix)

Arguments

Positional

ArgumentDescription
command... (required)Command and arguments to execute. Passed to sh -c as a single string unless -x is used.

Timing options

FlagDescription
-n, --interval SECSUpdate interval in seconds (default: 2.0, minimum: 0.1). Accepts fractional values.
-p, --preciseAttempt to run at precise intervals by compensating for command execution time

Display options

FlagDescription
-t, --no-titleHide the header line showing interval, command, and current time
-w, --no-wrapTruncate long lines at the terminal width instead of wrapping
-c, --colorInterpret and pass through ANSI color escape sequences
-C, --no-colorStrip ANSI color escape sequences from command output

Exit control options

FlagDescription
-b, --beepEmit a BEL character when the command exits with a non-zero status
-e, --errexitFreeze the display when the command exits with a non-zero status, wait for a keypress, then exit
-g, --chgexitExit when command output changes from the previous run
-q, --equexit CYCLESExit when command output remains unchanged for N consecutive cycles

Execution options

FlagDescription
-x, --execPass the command directly to exec() instead of sh -c. Arguments are not interpreted by a shell.

Behavior

Main loop

Each iteration performs the following steps:

  1. Query terminal dimensions.
  2. Execute the command (via sh -c or direct exec with -x).
  3. Clear the screen using ANSI escape sequences (ESC[2J ESC[H).
  4. Print the header line (unless -t is specified): Every N.Ns: command left-aligned with the current time right-aligned.
  5. Display the command output, truncated to the terminal height.
  6. If -w is specified, truncate each line at the terminal width.
  7. Sleep for the remaining interval time.

Precise mode (-p)

Without --precise, the interval is measured from the end of one execution to the start of the next. With --precise, the total cycle time (execution plus sleep) is kept as close to the requested interval as possible by subtracting the command’s execution time from the sleep duration. If the command takes longer than the interval, the next iteration starts immediately.

Exit conditions

  • Normal: runs indefinitely until interrupted (Ctrl-C).
  • --chgexit: exits with code 0 when the command output differs from the previous iteration.
  • --equexit N: exits with code 0 when the command output remains unchanged for N consecutive cycles.
  • --errexit: when the command exits non-zero, freezes the display with an error message, waits for a keypress, then exits with code 8.
  • --beep: sends a BEL character (\x07) when the command exits non-zero but continues running.

Exit codes

CodeMeaning
0Success, or exit triggered by --chgexit / --equexit
4Command could not be executed
8Command error with --errexit