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,241FansLike
391FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.
The wait is finally over (almost) for all you Ubuntu fans out there. The latest version of Ubuntu, 20.10 codenamed "Groovy Gorilla," is currently available in the beta version. I have tested out the distro myself, and it is stable enough to take out for a spin.

5 Best ways to encrypt files in Linux

Privacy is something that almost all of us have concerns about. Many people switch over to Linux because of its better privacy features. In this time, where many people believe (quite rightly so) that privacy doesn't exist anymore, Linux provides a ray of hope. Distributions like Tails OS are specifically designed for that purpose.

6 best task managers for Linux

One of the essential tools in any Linux distribution is a Task Manager. It is a system monitor application that gives you a report of all programs running on your computer and the status of your RAM and CPU usage.

What is FOSS, and how does it differ from Freeware

The rise of the Linux operating system, in all its various distributions, over the past few decades has catapulted the popularity of Free or Open Source Software (FOSS). Let's guide you in understanding what is FOSS, how it differs from freeware and is Linux a FOSS.

5 Best Application Launchers for Ubuntu

Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.