Linux watch Command


Introduction

The watch command in Linux is a powerful tool that allows you to execute a program periodically, showing output in fullscreen. This command runs the specified command repeatedly, displaying its output and errors. By default, the specified command runs every two seconds and watch runs until interrupted. In this article, we will explore the various options and uses of the watch command.

Syntax of watch command

The basic syntax of the watch command is −

$ watch [options] command

The watch command has several options that can be used to customize its behavior. These include −

  • -d, -differences − This option highlights the differences between successive updates. The options will be going to read the optional argument which changes highlight to be permanent, allowing the user to see what has changed at least once since the first iteration.

  • -n, -interval seconds − This option will specify an update interval. The command will not be going to allow quicker than the 0.1-second interval, in which the smaller values are getting converted.

  • -p, -precise − This option makes the watch attempt to run commands every interval seconds.

  • -t, -no-title − This option is used to turn off the header showing the interval, command, and the current time at the top of the display. It will also turn off the following blank line.

  • -b, -beep − This option will beep if the command has a non-zero exit.

  • -e, -errexit − This option will freeze the updates on command error, and exit after a key press.

  • -g, -chgexit − This option will exit when the output of the command changes.

  • -c, -color − This option interprets ANSI color and style sequences.

  • -x, -exec − This option command is given to sh -c which means that you may need to use extra quoting just to get the desired effect.

Use cases of watch command

The watch command has several use cases, such as −

Monitoring system resources

The watch command can be used to monitor system resources such as CPU usage, memory usage, and network traffic. This can be useful for identifying bottlenecks and troubleshooting performance issues.

$ watch -n 1 free -m

Monitoring log files

The watch command can be used to monitor log files in real-time. This can be useful for identifying errors and troubleshooting issues.

$ watch -n 1 tail -f /var/log/syslog

Advanced Usage of watch Command

In addition to the basic usage, watch command has many advanced options that can be used to customize the output and behavior of the command. These options can be used to highlight the differences between successive updates, specify the update interval, make watch attempt to run the command every interval seconds, turn off the header showing the interval, command, and the current time at the top of the display, give a beep if the command has a non-zero exit, freeze the updates on command error, exit when the output of command changes, interpret ANSI color and style sequences, and execute the command given to ‘sh -c’.

The following are some examples of advanced usage of watch command −

Highlighting the Differences − The -d or -differences option can be used to highlight the differences between successive updates. The options will be going to read the optional argument which changes highlight to be permanent, allowing the user to see what has changed at least once since the first iteration.

$ watch -d df

Specifying the Update Interval − The -n or -interval seconds option can be used to specify the update interval. The command will not be going to allow quicker than the 0.1-second interval, in which the smaller values are getting converted.

$ watch -n 1 df

Attempt to run command every interval seconds − The -p or -precise option can be used to make watch attempt to run the command every interval seconds.

$ watch -p df

Turning off the Header − The -t or -no-title option can be used to turn off the header showing the interval, command, and the current time at the top of the display. It will also turn off the following blank line.

$ watch -t df

Giving a Beep − The -b or -beep option can be used to give a beep if the command has a non-zero exit.

$ watch -b df

Freezing the Updates on Command Error − The -e or -errexit option can be used to freeze the updates on command error, and exit after a key press.

$ watch -e df

Exiting when the Output of Command Changes − The -g or -chgexit option can be used to exit when the output of command changes.

$ watch -g df

Interpreting ANSI Color and Style Sequences − The -c or -color option can be used to interpret ANSI color and style sequences.

Executing the Command Given to sh -c − The -x or –exec option can be used to execute the command given to sh -c which means that you may need to use extra quoting just to get the desired effect.

$ watch -x "free -m"

Conclusion

In this article, we have discussed the watch command in Linux and its various options. The watch command is a useful tool for monitoring the output of a command periodically. The command can be used with various options to customize the output and behavior, such as highlighting differences, specifying update intervals, and exiting on command errors. This can be particularly useful for monitoring system resources, processes, and network activity in real-time.

Updated on: 13-Feb-2023

995 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements