LinuxCommandLibrary

watch

Execute a command periodically and display output

TLDR

Repeatedly run a command and show the result

$ watch [command]
copy

Re-run a command every 60 seconds
$ watch [[-n|--interval]] 60 [command]
copy

Monitor disk space, highlighting differences as they appear
$ watch [[-d|--differences]] [df]
copy

Repeatedly run a pipeline and show the result
$ watch "[command_1] | [command_2] | [command_3]"
copy

Exit watch if the visible output changes
$ watch [[-g|--chgexit]] [lsblk]
copy

Interpret terminal control characters
$ watch [[-c|--color]] [ls --color=always]
copy

SYNOPSIS

watch [options] command

PARAMETERS

-d, --differences
    Highlight changes between successive updates. Use `-d=cumulative` to highlight all changes since the first update.

-n <seconds>, --interval=<seconds>
    Specify the update interval in seconds. Default is 2 seconds.

-t, --no-title
    Turn off the header showing the command, interval, and current time.

-e, --chgexit
    Exit watch immediately if the output of the command changes.

-x, --exec
    Execute the command via `sh -c`. This is crucial when the command contains shell metacharacters like pipes (`|`) or redirections (`>`, `<<`).

-c, --color
    Interpret ANSI color and style sequences.

-p, --precise
    Attempt to run the command exactly on the specified interval, rather than waiting for the command to finish.

DESCRIPTION

watch is a command-line utility in Linux that allows you to execute a specified command repeatedly and display its output on the console. It's particularly useful for monitoring changes in system status, file contents, or the output of other commands over time without manually re-executing them.

By default, watch clears the screen and redisplays the output every two seconds. It can also highlight the differences between successive updates, making it easy to spot changes. Users can specify the refresh interval, choose not to display the header, or exit when the command's output changes. It's an excellent tool for real-time system monitoring, debugging, and observing dynamic data.

CAVEATS

watch is not suitable for commands that produce side effects (e.g., modifying files, deleting data) or require interactive input, as it executes the command repeatedly without user intervention.

Output buffering by the watched command can sometimes delay updates within watch. Using tools like `unbuffer` (from the `expect` package) or `stdbuf -oL` may help in such cases.

Complex commands involving shell features (pipes, redirections) often require being enclosed in quotes and used with the `-x` (--exec) option for correct execution.

EXIT STATUS

The watch command typically returns the exit status of the last execution of the watched command. If watch itself encounters an error (e.g., the command specified cannot be found), it usually returns an exit status of 255.

COMMAND QUOTING

When the command argument to watch contains spaces, shell metacharacters (e.g., `|`, `>`, `<`, `&`), or refers to shell built-ins, it must be enclosed in single or double quotes. For complex commands involving pipes or redirection, the `-x` (--exec) option is essential to ensure the command is executed by `sh -c`, allowing proper shell interpretation.

HISTORY

watch is part of the `procps` (or `procps-ng`) package, a collection of utilities for controlling and monitoring processes in Linux. It has been a standard utility in Unix-like systems for many years, providing a simpler alternative to manual re-execution or shell loops like `while true; do ...; sleep N; done`.

SEE ALSO

sleep(1), tail(1)

Copied to clipboard