How to Use the Linux watch Command

Linux Watch Command

In this tutorial, we’ll be going over the Linux watch command and how we can utilize it in our Linux administration work. Essentially, the watch command is a utility that allows us to continually execute a command at specific intervals and monitor the differences between the outputs. Why could this be useful? For a lot of reasons. But let’s how to use it, go over the options of the Linux watch command, and then understand the need for the command.

1. Basics of the Linux watch Command

Without arguments, the watch command in Linux will execute the specified command every 2 seconds until it is interrupted. We can customize the interval and quite a few things within the output. Let’s begin with the basics of the watch command.

Syntax:

watch <command>

For the demonstration, I’ll run the hwclock command which displays the hardware clock with the watch command. Here’s what the output looks like.

watch hwclock
Watch Command Full
Watch Command Full

The first line displays the time interval and the command that’s going to be run. The second line displays output from the command. On the far right side, the command also displays the current system time. Simple? Okay, let’s move ahead now.

2. Set Custom Interval with watch Command in Linux

We know that by default, the command runs every 2 seconds. To set a custom interval with the Linux watch command, we need to use the -n option which allows us to specify the interval in seconds.

watch -n <seconds> <command>

I’ll continue to use the hwclock as an example throughout the tutorial for demonstrating the watch command. Let’s set the time interval to 10 seconds.

watch -n 10 hwclock
Watch Custom Interval
Watch Custom Interval

If you want the commands to run at precise intervals (accounting for milliseconds) instead of the regular time interval, we can add the -p command option as shown below.

watch -p -n 10 hwclock

3. Hide the watch Command Header

Notice the header that’s always displayed in the first line of the command output? If you do not want that line to show and only want to see the output from the command, you can use the -t or –no-title option.

watch --no-title hwclock

This command will only display the output from the hwclock command and hide the header displaying the interval and the command that’s being run.

4. Highlight Changes with the Linux watch Command

Be default, the watch command cleans the entire screen and runs the command again to display the new output. That’s fine if you just want to view the latest information on the terminal screen but it isn’t much help when you need to view the updates that happened.

That’s where you can use the -d command to see what changed since the last run.

watch -d hwclock
Linux Watch command Highlight Changes
Watch Highlight Changes

This will clear the highlights in the next run if there are no changes. If you want the highlights to stay put, you can use the -d=cumulative option.

watch -d=cumulative hwclock
Watch Highlight Cumulative
Watch Highlight Cumulative

In comparison to the image above, you will notice that the “5” in “58” stays highlighted. If we were using only the -d option, the “5” would not be highlighted after the 50th second. Also, to demonstrate the cumulative highlight, I shortened the time interval to 0.2 seconds.

5. Other Options for the watch Command in Linux

There are a few other options that can be used with the Linux watch command though they won’t be used as frequently as the ones discussed above.

  • -b, –beep Beep if the command exits with a non-zero value
  • -e, –errexit Exit if the command exits with a non-zero value
  • -g, –chgexit Exit when the command refreshes

Conclusion

We hope this short tutorial on the Linux watch command proved useful to you. To learn more about the watch command in Linux, use the man command.