watch command in Linux with Examples
Last Updated :
09 Sep, 2024
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.
Similar Reads
usermod command in Linux with Examples
usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
4 min read
username Command in Linux With Examples
Linux as an operating system holds the capabilities of handling multiple users each with a username and a display name (Full Name). So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added it
4 min read
users command in Linux with Examples
users command in Linux system is used to show the user names of users currently logged in to the current host. It will display who is currently logged in according to FILE. If the FILE is not specified, use "/var/run/utmp". "/var/log/wtmp" as FILE is common. Syntaxusers [OPTION]... [FILE]where,OPTIO
2 min read
How to Delete User in Linux | userdel Command
Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel
5 min read
vi Editor in Linux
The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor. Table of C
9 min read
vmstat command in Linux with Examples
vmstat command in Linux/Unix is a performance monitoring command of the system as it gives the information about processes, memory, paging, block IO, disk, and CPU scheduling. All these functionalities makes the command vmstat also known as virtual memory statistic reporter. The very first report pr
3 min read
vnstat command in Linux with Examples
vnstat is a command-line tool in Linux that is generally used by system administrators in order to monitor network parameters such as bandwidth consumption or maybe some traffic flowing in or out. It monitors the traffic on the system's network interfaces. Installing vnstat on LinuxIn case of RedHat
4 min read
w command in Linux with Examples
The 'w' command in Linux gives us important information about who is currently using the computer, how much the computer is being used, and what programs are running. It's a handy tool for people who take care of computer systems, as it helps them keep an eye on what users are doing, how much of the
3 min read
wall command in Linux with Examples
wall command in Linux system is used to write a message to all users. This command displays a message, or the contents of a file, or otherwise its standard input, on the terminals of all currently logged in users. The lines which will be longer than 79 characters, wrapped by this command. Short line
3 min read
watch command in Linux with Examples
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' r
4 min read