Home Learn Linux 7 ways to use the Linux Watch command

7 ways to use the Linux Watch command

by Habib Ahmed
Linux Watch Command

On several occasions, you may need to run a command or utility repeatedly after some time interval. We can use specific cron jobs with the help of bash scripting or using other programming languages. However, Linux has a built-in watch command that is used to run other commands on a regular interval, and then it displays the output in the terminal. The watch utility is pre-installed on nearly all Linux distributions.

Linux Watch Command with examples

In this tutorial, We will show you seven different ways to use the Linux watch command.

1. Run Command Every 2 Seconds

By default, the watch command runs after every 2 seconds and shows output on the terminal. It keeps running until interrupted by the user.

Syntax:

watch <option> <command>

Where <option> are the parameters which we will discuss later in this article, and <command> is another command that you want to execute by watch utility.

watch date

Example:

watch command example

watch command example

2. Run Command Every N Seconds

Watch command update interval can be overridden. It means you can change the update interval from 2 seconds to your desired interval as well.

Syntax:

watch -n <interval-in-seconds> <command>

Where <interval-in-seconds> is update interval in seconds and <command> is another command you want to execute by watch utility.

watch -n 5 date

Example:

watch command N interval example

watch command N interval example

3. Highlighting The Differences Between Updates

You can also highlight the differences between previous output and current output. So instead of reading the whole output, you can keep an eye on the changes.

Syntax:

watch -d <command>

However, if the interval between the updates is very short, it will be challenging to review differences, so you must set a reasonable update interval.

watch -n 5 -d date

Example:

watch command difference example

watch command difference example

4. Hide Header in output

Watch command output shows a header containing time interval, command, system name, and system date. However, if you do not want to see this portion, you can hide it as well.

Syntax:

watch -t <command>
watch -t date

Example:

watch command without header example

watch command without header example

5. Beep On Error

The watch command can also give a beep sound if an update fails. For this to happen, first, you may need to check if the beep package is installed or not. In case it is not installed, you can install it using the following command:

sudo apt install beep

Syntax:

watch -b date

6. Exit On Change

By default, the watch command keeps running until it is interrupted manually by the user. However, You can make it exit if it identifies a change in output as well.

Syntax:

watch -g <command>
watch -n 10 -g date

Example:

watch command exit on change example

watch command exit on change example

Now watch exit; it returns a certain status that can be got by using the below command:

echo $?

watch command show exit status

As you can see in the above screenshot, it displays certain integer flags starting from 0 to 8. Each of these flags has its meaning, and details of these flags are available on the man page.

man watch
watch command exit status definitions

watch command exit status definitions

7. Using Big Commands

You can use other commands with watch command in a cleaner way.  There are multiple ways to do that. You can use \ sign after the watch command and its options.

watch -n 10 -d \

Example:

watch command running bg commands by backslash example

watch command running bg commands by backslash example

You can also write you complete custom command inside single quotation marks as well.

watch <options> '<commands>'

Example:

watch command running significant commands by quotation mark example

Conclusion

By now, you should have a good understanding of watch command and its importance, especially when you want to monitor your system’s resources like hard disk, network changes, CPU usage, etc. You can get more information about the watch command on its man page. Use command man watch in your terminal to view the man page.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.