How to Use the Linux watch Command with Examples

Introduction

To repeatedly run a command or job in regular time intervals while working in Linux, you can use cron jobs or bash scripts. However, Linux also offers a more straightforward, built-in solution – the watch command.

In this tutorial, you will learn the watch command syntax, how it works, and the different things it can help you do.

How to use the Linux watch command with examples

Prerequisites

Linux watch Command Overview

The watch command is a built-in Linux utility used for running user-defined commands at regular intervals. It temporarily clears all the terminal content and displays the output of the attached command, along with the current system date and time.

By default, the watch command updates the output every two seconds. Press Ctrl+C to exit out of the command output.

The watch command is useful when you need to monitor changes in a command output over time. This includes disk usage, system uptime, or tracking errors.

Linux Watch Command Syntax

The watch command uses the following syntax:

watch [option] [command]

Where:

  • [option]: Adding an option changes the way the watch command behaves. Available options are listed below.
  • [command]: A user-defined command you want to run repeatedly.

The watch command options include:

-n, --interval Allows you to specify the interval between output updates.
-d, --differences Highlights the differences between output updates.
-g, --chgexit Exits the watch command when the output of the user-defined command changes.
-t, --no-title Removes the header showing the interval, command, and current time and date.
-b, --beep Plays a sound alert (beep) if the command exits with an error.
-p, --precise Attempts to run the command after the exact number of seconds defined by the --interval option.
-e, --errexit Stops output updates on error and exits the command after a key press.
-c, --color Interprets ANSI color and style sequences.
-x, --exec Passes the user-defined command to exec, reducing the need for extra quoting.
-w, --no-linewrap Turns off line wrapping and truncates long lines instead.
-h, --help Displays help text and exits.
-v, --version Displays version information and exits.

Linux Watch Command Examples

Here are some of the ways you can use the watch command options to achieve different results:

Run Command with a Custom Interval

Set a custom interval to run a user-defined command and show the output by using the -n or --interval option:

watch -n [interval in seconds] [command]

For instance, to display the system time and date every 5 seconds, run:

watch -n 5 date
Use the -n option to set a custom interval

Note: The -n option allows you to use fractions of a second, with a minimum interval of 0.1 seconds. When entering decimals, both a period (.) and a comma (,) work for any locale.

Highlighting Changes Between Updates

Use the -d or --difference option to highlight changes between successive output updates:

watch -d [command]

For example, display the system date and time in the default 2-second interval with the changes highlighted:

watch -d date
Use the -d option to highlight changes between outputs

Pass =cumulative to the -d option if you want all the values that have ever changed to stay highlighted:

watch -d=cumulative date

Exit on Change

The -g or --chgexit option causes the watch command to exit if there is a change in the output:

watch -g [command]

As an example, adding the free command monitors your system’s memory consumption and exits if the value changes:

watch -g free
Use the -g option to exit the watch command if the output changes

Hide the watch Command Header

Turn off the header containing the interval time, user-defined command, and current system time in the watch command output by using the -t or --no-title option:

watch -t [command]

Returning to the example of displaying the system date and time, this time without the header:

watch -t date
Use the -t option to display the watch command output without the header

Alert on Error

The watch command uses the beep package to play a sound alert if the output update fails due to an error. To do this, use the -b or --beep option:

watch -b [command]

Note: if you don’t have the beep package installed, add it with sudo apt install beep command.

Using Complex Commands

The watch command also allows you to use more complex user-defined commands, with their own arguments and options. One way to do this is to use the backslash (‘/’) symbol:

watch [options] /

Using the command above brings you to the next line in the terminal, where you need to add the user-defined command. Once you hit Enter, it executes the command. For instance:

watch -n 5 /
echo "watch command example output"
Adding a complex command using the backslash symbol

Another option is to add the user-define command in single quotation marks:

watch [options] '[command]'

Using the example above, the command would be:

watch -n 5 'echo "watch command example output"'
Adding a complex command using single quotation marks

Conclusion

After reading this tutorial, you should have a better understanding of how the watch command works and what you can use it for.

For a more comprehensive overview of Linux commands, check out our Linux command cheat sheet.

原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/224444.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

相关推荐

发表回复

登录后才能评论