How to use the command 'watch' (with examples)
The watch
command is a powerful tool for users of Unix-like operating systems who need to execute a program or script periodically, with the output displayed on the entire screen. This utility is especially useful for tasks that require continuous monitoring or when the user wants to see how the output of a command evolves over time. Not only does watch
facilitate the automation of repetitive command execution, but it also highlights changes, making it easier to spot new data or modified states.
Use case 1: Repeatedly run a command and show the result
Code:
watch command
Motivation:
This fundamental use case of the watch
command is especially beneficial for users who need constant updates on a particular system state or resource. For instance, if you’re interested in monitoring CPU usage, you might repeatedly run the top
or uptime
command. By refreshing the output every two seconds (the default interval), watch
allows you to observe changes live, eliminating the need for manually re-entering the command continually.
Explanation:
watch
: The primary command that sets up the periodic execution.command
: Represents any command you wish to run repeatedly. It could be basic likedate
or something more comprehensive likedf -h
to check disk usage.
Example Output:
Let’s say we use watch date
to display the current date and time. The output would refresh every two seconds, akin to:
Every 2.0s: date
Mon Oct 16 14:52:03 UTC 2023
As time progresses, the displayed time updates, providing a real-time clock in the terminal.
Use case 2: Re-run a command every 60 seconds
Code:
watch -n 60 command
Motivation:
There are instances when refreshing the command output every two seconds might be too frequent, causing unnecessary resource usage and cluttering of information. Suppose you are monitoring the battery percentage of a laptop, which doesn’t change every second. Setting a longer interval, such as 60 seconds, using the -n
option, makes more sense as it aligns better with the rate of change you expect to observe.
Explanation:
watch
: Executes the command at regular intervals.-n 60
: This option specifies the interval between command executions. By using60
, this tellswatch
to re-run the command every 60 seconds.command
: Any command to be executed periodically.
Example Output:
When you run watch -n 60 free -m
, which shows memory usage, suppose the displayed output might look like:
Every 60.0s: free -m
total used free shared buff/cache available
Mem: 7984 1203 5628 12 1152 6439
Swap: 2047 0 2047
The memory status is updated every minute, enabling you to notice memory usage trends over time.
Use case 3: Monitor the contents of a directory, highlighting differences as they appear
Code:
watch -d ls -l
Motivation:
Monitoring directory contents is crucial for developers and system administrators dealing with dynamic file systems where files are frequently added, removed, or modified. By leveraging the -d
flag in conjunction with a directory listing command like ls -l
, watch
highlights differences in file states, allowing users to quickly identify changes without having to resort to other file tracking systems.
Explanation:
watch
: Executes commands regularly and displays the output.-d
: The difference highlighting option, where changes in output since the last execution are prominently shown.ls -l
: A detailed listing of directory contents.
Example Output:
Running watch -d ls -l
in a directory results in an output like:
Every 2.0s: ls -l
total 8
-rw-r--r-- 1 user user 345 Oct 16 12:00 file1.txt
-rw-r--r-- 1 user user 675 Oct 16 12:00 file2.txt
If file1.txt
was modified or a new file was added, the differences would be highlighted, making it quite easy to spot recent changes.
Use case 4: Repeatedly run a pipeline and show the result
Code:
watch 'command_1 | command_2 | command_3'
Motivation:
Complex data processing or filtering often requires the use of command pipelines, combining multiple commands to achieve the desired output. Suppose you are managing logs and want to filter and format them in real-time. Running a pipeline command at regular intervals will keep the terminal updated with relevant information, providing efficient and continuous feedback from the system.
Explanation:
watch
: Facilitates the repeated execution of the specified pipeline.'command_1 | command_2 | command_3'
: A series of piped commands enclosed in single quotes, where the output ofcommand_1
is passed tocommand_2
, and so on. This allows for more complex operations to be observed repeatedly.
Example Output:
Consider using watch 'ps aux | grep python | wc -l'
to monitor the number of Python processes running. The output might look like:
Every 2.0s: ps aux | grep python | wc -l
3
This example would show the count of Python processes every two seconds, useful for ensuring that scripts or applications manage processes as expected.
Conclusion:
The watch
command is a versatile tool for Unix-like system users needing periodic updates on various system and application states. Whether checking system statistics, observing file changes in directories, or running complex command pipelines, watch
simplifies processes through automatic, continuous execution, enabling users to focus on analyzing fresh, relevant data efficiently.