Dec 04, 2023
Edward S. & Ignas R.
What Is the Linux watch Command, How to Use It + Examples
There are plenty of monitoring and troubleshooting tools available for Linux VPS. However, many are difficult to install or have a steep learning curve.
This is where the Linux watch command comes in handy. It’s a built-in Linux command that lets users execute a specific command repeatedly and display the result in a standard output on a console or terminal window.
This tutorial will explain the Linux watch command and provide several use cases.
What Is the watch Command?
The Linux watch command is a built-in command-line tool that runs user-defined commands at regular intervals. The command is especially helpful for users who wish to monitor changes in their command output over time. It’s most commonly used to track system uptime, memory, or disk usage. The Linux watch command will run simultaneously in the terminal until stopped by pressing CTRL+C.
By default, the Linux watch command runs the user-defined command every two seconds. However, users can easily change the interval by changing its options.
Additionally, when combined with different pipeline commands, watch lets users monitor the creation and deletion of files. It is useful when users want to troubleshoot an issue or continuously monitor the script output.
Linux watch Command Syntax
The watch command syntax looks like this:
watch [options] [your command]
For example, a watch command combined with the free command would look like this:
The specified command shows that the watch command clears the terminal window and runs the free command every two seconds. You can find the list of most commonly used watch command options below:
|-d, –differences||Displays the differences between watch command updates. If you want all changed values to stay highlighted, use the combined -d=cumulative option.|
|-t, –no-title||Turns off the header that displays the time interval, command, current time and date, and hostname.|
|-n, –interval||Allows users to specify the time interval between output updates in seconds.|
|-b, –beep||Plays a beep whenever a command exits with an error.|
|-g, –chgexit||Exits the watch command when the user command output changes.|
|-e, –errexit||Halts watch command updates on screen when an error occurs and exits the command after a key press.|
|-h, –help||Displays the help page.|
|-c, –color||Interprets ANSI color and style sequences.|
|-x, –exec||Forwards user command to exec.|
|-p, –precise||Tries to run the user-defined command exactly after seconds defined by the –interval option.|
Linux watch Command Examples
Check out some of the watch command use cases. Start by logging in to your virtual private server.
Using watch Command to Highlight the Differences Between Updates
watch regularly updates the specified command’s output. To view the changing output, use the –d or –differences option, as it will highlight the changes:
watch -d free
Here, the RAM usage values are highlighted in the command line:
Using watch Command to Change Time Intervals
Users can effortlessly change the duration between outputs with the -n option followed by a digit representing the number of seconds. For example:
watch -n 5 date
This command will reflect the updated date command output after every five seconds. Remember that Linux watch can’t observe regular intervals of less than 0.1 seconds.
Using watch Command to Exit After a Single Change
It’s often useful for the watch command to exit after one change in the output. Users can achieve this with the help of the –g option. An example would look like this:
watch -g free
The –g option stops the specified command whenever there are changes in memory consumption.
Using watch Command to Hide Command Header
It’s possible to turn off the header that shows a blank line, current time, command, and interval with the help of the –t option. For example:
watch -t free
How to Use the Linux watch Command with Other Pipeline Commands
The watch command can also be helpful with other pipeline commands:
Using watch Command to Monitor Files
To view a particular file status or its changes, use the following command:
watch -d 'ls -l | fgrep newuser'
Here, the newuser is the username, and ls -l represents the files in the user directory.
Using watch Command as a Clock
To use the watch command as a clock, execute the specified command below:
watch –t date
The executed date command will display the clock and date only.
Using watch Command to View Network Status
It’s possible to use the watch command to monitor your network. For example:
watch "netstat -anp | grep -c ':22\b.*LISTEN'"
The command will display active connections on port 22.
You can also pipe the tail command to retrieve specific output. For example, the following runs netstat every two seconds and prints the last three network logs:
watch -n 2 "netstat -ant | tail -3"
Troubleshooting and tracking changes are important parts for any Linux user, which is where the watch command comes in.
It makes it easy for users to observe various command output changes without running them repeatedly.
In this tutorial, we’ve covered the basics of the watch command and provided some excellent use cases for it.
We hope that you found this tutorial useful. If you have any questions or suggestions, leave them in the comments section below.
Discover Other Linux Commands for Server Management
How to Check Disk Space on Linux
How to Use Linux Screen for Effective Terminal Management
How to Transfer Data With curl Command
How to Calculate Process Execution With the time Command
How to Transfer Files Using the scp Command
How to Shutdown and Restart a Server
How to Transfer and Synchronize Data With rsync
How to Manage and List Services in Linux
How to Use the Linux Grep Command