Linux Watch Command
Introduction
Before we discuss Linux watch command, let's understand-What is a Watch Command ?
watch
runs any arbitrary command at regular intervals and displays the command's output in the terminal window.
It comes in handy when you need to run a command several times and see the result vary over time. The watch
command, for example, can be used to keep track of system uptime or disk consumption.
The watch
program comes pre-installed on nearly all Linux distributions as part of the procps
(or procps-ng
) package.
In this tutorial, we will talk about the watch
command. We will also address a few FAQs on Linux watch command.
How to Use the watch
Command
The watch
command has the following syntax:
watch [OPTIONS] COMMAND
Let's use the date
command to demonstrate how the watch
command works:
watch date
The watch
command, as shown in the image above, will clear all terminal content and start running the specified command at regular intervals. Watch
will run the supplied command every two seconds if no options are specified.
The watch
update interval and the executed command (Every 2.0s: date
) are displayed on the top left side of the screen header, while the top left side watch displays the current time and date. The -t
(--no-title
) option is used to disable the header.
The screen displays the output of the supplied command, which is refreshed every two seconds.
Simply press the Ctrl+C
key combination to exit the watch
command. You can also use the -g
(--chgexit
) option to have watch
exit when the output from the command changes.
We'll go over the most popular watch
command choices in the sections below.
How to Change the Time Interval
What if the two-second update interval isn't enough for your application?
You can adjust the time interval between updates by using the -n
(--interval
) option followed by the appropriate number of seconds:
watch -n INTERVAL_IN_SECONDS COMMAND
For example, to use the df command to monitor your disk space utilization and refresh the screen every five seconds, type:
watch -n 5 df -h
Highlighting the Difference Between Updates
With the -d
(--difference
) option, watch
will highlight the differences between updates.
watch -d COMMAND
Let's imagine you wish to use the uptime command to track system uptime and highlight any changes. The command would be as follows:
watch -d uptime
Pass =cumulative
to the -d
option to make the highlights persistent. This means that all previously altered values will remain highlighted.
watch -d=cumulative COMMAND
Commands with Pipes
You must encapsulate a command that contains pipes in single or double quotes in order to execute it. If you don't include the entire command, watch
will only run the first command and then pipe the output to the following command in the pipeline.
watch 'COMMAND_1 | COMMAND_2'
Using a combination of the netstat
and grep programs, for example, the following command will monitor the number of active connections on port 80
:
watch "netstat -anp | grep -c ':80\b.*LISTEN'"
FAQs on Linux Watch Command
How do I use the watch
command?
To use watch
, simply type watch
followed by the command or script you want to monitor. For example, watch ls -l
will display the output of the ls -l
command at regular intervals.
What is the default refresh interval of the watch
command?
The default refresh interval of the watch
command is 2 seconds. It updates and displays the output every 2 seconds.
What is the default refresh interval of the watch
command?
The default refresh interval of the watch
command is 2 seconds. It updates and displays the output every 2 seconds.
Can I change the refresh interval of the watch
command?
Yes, you can change the refresh interval by using the -n
or --interval
option followed by the desired number of seconds. For example, watch -n 5 ls -l
will update the output every 5 seconds.
What happens if the interval is set to 0 in the watch
command?
If the interval is set to 0 (watch -n 0 command
), the watch
command will update as fast as possible without any delay. This can lead to high CPU usage and may not be suitable in most cases.
Can I exclude specific parts of the output from being updated with watch
?
Yes, you can use additional commands, such as grep
, to filter and exclude specific parts of the output from being updated in the watch
command. For example, watch 'ls -l | grep myfile'
will only display updates when the file "myfile" changes.
How can I stop the watch
command from running?
You can stop the watch
command by pressing Ctrl+C
in the terminal where watch
is running. This will terminate the watch
process and stop the command from executing further.
Conclusion
You should now be able to use the Linux watch
command with confidence. By running man watch
on your terminal, you can see all the possible watch
command options at any time.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.