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

-n, --interval
    Specify update interval in seconds. The command is executed every seconds (default is 2).

-d, --differences[=]
    Highlight the differences between successive updates. permanent makes highlight permanent.

-t, --no-title
    Turn off showing the header at the top of the display.

-h, --help
    Display help text and exit.

-v, --version
    Display version information and exit.

-x, --exec
    Pass command to sh -c which expands variables. This is needed for commands such as watch ls ${DIR}.

-e, --errexit
    Exit if the command has a non-zero exit status.

-b, --beep
    Beep if the command has a non-zero exit status.

DESCRIPTION

The watch command in Linux executes a specified program at regular intervals and displays its output on the terminal. It's invaluable for monitoring system resources, log files, or any command whose output changes over time. By default, watch updates the display every 2 seconds, but this interval can be adjusted. The command highlights the differences between successive executions, making it easy to spot changes. The watch command is particularly useful for system administrators and developers who need to keep a close eye on system activity. It provides a simple way to track changes without manually re-running commands. It is easy to use and offers a simple way to view dynamic processes or log file changes. You can configure many options such as the interval time or even if the changes are highlighted or not. It is very useful for tracking changes as they occur in real-time, providing immediate visibility into the evolving state of the system.

INTERRUPTING WATCH

To stop the watch command, press Ctrl+C.

HIGHLIGHTING NUANCES

The -d option with the =permanent argument makes the highlight stay on the screen and doesn't reset itself with new changes.

HISTORY

The watch command has been a part of Unix-like systems for a long time, providing a simple and effective way to monitor changes in system processes and outputs. It's a staple in system administration and development environments.

SEE ALSO

tail(1), top(1), ps(1)

Copied to clipboard