Open In App

watch command in Linux with Examples

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The 'watch' command in Linux is a powerful utility that allows you to execute a command periodically, displaying its output in fullscreen mode. It is particularly useful for monitoring the output of commands that change over time, such as system resource usage or server status. By default, 'watch' runs the specified command every 2 seconds, continuously updating the display until interrupted.

Here, we will cover the syntax, options, and practical examples of the 'watch' command, helping you utilize it effectively in your Linux environment.

What is the 'watch' Command?

The 'watch' command runs another command repeatedly, showing its output and errors, and is especially useful for tracking the real-time status of commands that produce frequently changing outputs. This command keeps running until you manually stop it, usually by pressing 'Ctrl + C'. It is a handy tool for system administrators, developers, and anyone needing to observe the behavior of a command over time.

Syntax:

watch [options] command

Commonly Used Options with watch

1. -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.

Example:

watch -d  free -m

2. -n, --interval seconds:

This option will specify 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.

Example:

watch -n 1 free -m

3. -p, --precise:

This option make watch attempt to run command every interval seconds.

Example:

watch -p free -m

4. -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.

Example:

watch -t free -m

5. -b, --beep:

This option will give beep if the command has a non-zero exit.

Example:

watch -b free -m

6. -e, --errexit:

This option will freeze the updates on command error, and exit after a key press.

Example:

watch -e free -m

7. -g, --chgexit:

This option will exit when the output of command changes.

Example:

watch -g free -m

8. -c, --color:

This option interprets ANSI color and style sequences.

watch -c ls --color

9. -x, --exec:

This option command given to 'sh -c' which means that you may need to use extra quoting just to get the desired effect.

watch -x 'df -h | grep /dev/sda1'

10. watch -h:

This option will show the help message and exit.

watch -h

11. watch -v:

This option will display the version information and exit.

watch -v

Conclusion

The 'watch' command is a versatile tool in Linux that allows users to execute and monitor commands periodically. Its ability to highlight differences, run commands at specified intervals, and alert on errors makes it an essential utility for system monitoring and troubleshooting. By leveraging the various options and understanding its use cases, you can enhance your command-line experience and make real-time monitoring more efficient.


Next Article

Similar Reads