7 ways to use the Linux Watch command

Are you interested in learning the Watch command in Linux? We have put together important details of the Linux Watch command along with examples.

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.

Habib Ahmed
He is a professional writer with more than seven years of experience in the field. He also enjoys technical writing/ manuals, white papers, and scientific presentations.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,054FansLike
407FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

The hierarchy tree of Linux Mint makes it an Ubuntu-based Debian-based Linux distribution that is community-driven. This historical and developmental attribute of this Linux distro makes it an ideal candidate to offer free and open-source bundled applications to a vast range of its Linux community users. Additionally, after completing out-of-the-box multimedia support, users directly benefit from its proprietary software support through multimedia codecs.
The innovative strides of email services came as a faster alternative for the postal services. However, it does not imply that the growing use and embrace of email services have contributed postal services to the messaging world obsolete, for not all messages are transmitted through words.