How to Use the 'watch' Command (with Examples)

How to Use the 'watch' Command (with Examples)

The ‘watch’ command in Unix-like operating systems provides a convenient way to repeatedly execute a specified command at regular intervals. Its primary function is to help users monitor command output in real-time, refreshing the display continuously. This is particularly useful when attempting to track changes or updates over time without manually re-executing a command. The command runs in full-screen mode, making it easier for users to keep a close eye on dynamic data. With options to customize the refresh interval and alert on changes, the ‘watch’ command is a versatile tool for monitoring system processes, file directories, and more.

Use case 1: Monitor files in the current directory

Code:

watch ls

Motivation:

Sometimes, when working in a directory where files are frequently added, deleted, or modified, it’s essential to keep track of these changes as they happen. Using the ‘watch’ command with ’ls’, which lists the contents of a directory, enables you to get an instantaneous update on the status of files. This setup is particularly useful in environments like web development, where assets might be updated regularly, or in collaborative settings, where multiple users contribute to the same project directory.

Explanation:

  • watch: Invokes the watch command to repeatedly run a specified command.
  • ls: Lists the files in the current directory. By combining this with ‘watch’, the files will be listed repeatedly at set intervals, refreshing the view so any changes become immediately apparent.

Example Output:

Every 2.0s: ls  Mon Sep 12 14:44:41 2023

file1.txt     file2.txt     newfile.jpg   script.py

Use case 2: Monitor disk space and highlight the changes

Code:

watch -d df

Motivation:

Monitoring disk space usage is crucial, especially in systems where storage capacity is limited or when performing operations that may consume substantial disk space, such as backups or installations. The ‘watch’ command, when used with ‘df’ (which reports the amount of available disk space), helps in keeping an eye on disk usage over time. The ‘-d’ flag is particularly beneficial as it highlights differences or changes in the disk usage between updates. This visual cue makes it easier to spot any significant changes in usage, facilitating proactive management of disk space.

Explanation:

  • watch: Initiates repeated execution of a given command.
  • -d: The ’d’ flag stands for ‘differences’; it highlights changes detected between the execution intervals, making it straightforward to detect alterations.
  • df: The command used to check disk space on mounted filesystems. Combined with ‘watch’, it allows for real-time monitoring of space changes.

Example Output:

Every 2.0s: df  Mon Sep 12 14:46:05 2023

Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1       10230840  5023528   4663532  52% /
[df output continues...]

Use case 3: Monitor “node” processes, refreshing every 3 seconds

Code:

watch -n 3 "ps aux | grep node"

Motivation:

For developers or system administrators working with Node.js applications, it’s often vital to monitor the processes related to Node.js to ensure they are running correctly or to debug potential issues. This setup can provide insights into the resource consumption of Node.js processes and help verify whether these processes are working as expected. By setting the interval to 3 seconds, users can get frequent updates without overwhelming the system with constant requests.

Explanation:

  • watch: Starts the process for repeatedly executing a command.
  • -n 3: Specifies that the command should refresh every 3 seconds, allowing updates to be viewed in close to real-time.
  • "ps aux | grep node": The enclosed command is used to list all processes (ps aux) and then filter them to show only those associated with ’node’ by using grep. This helps specifically target Node.js processes.

Example Output:

Every 3.0s: ps aux | grep node  Mon Sep 12 14:47:30 2023

user    12345  0.5  2.4  234567  123456 ?  Sl   14:42   0:05 /usr/bin/node server.js
user    54321  0.1  1.6  123456  65432 ?  S    14:40   0:01 /usr/bin/node worker.js

Use case 4: Monitor disk space and if it changes, stop monitoring

Code:

watch -g df

Motivation:

In scenarios where constant monitoring isn’t necessary, but alerts upon change are, using ‘watch’ with the ‘-g’ flag and ‘df’ can be exceptionally beneficial. This approach is particularly useful during periods where disk operations might unpredictably vary or when precise timing of changes needs to be captured. The ‘watch -g’ command will halt execution and notify the user when a change is detected in disk space, providing a quick intervention point without resource-intensive constant monitoring.

Explanation:

  • watch: Operates the command repeatedly.
  • -g: The ‘g’ flag stands for “exit on change.” It modifies ‘watch’ to cease execution as soon as it detects a change in the command output, which is practical for one-time alerts.
  • df: The disk space monitoring command here tracks changes in disk usage, crucial in managing storage solutions or spotting unexpected disk space consumption.

Example Output:

Every 2.0s: df  Mon Sep 12 14:49:50 2023

Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1       10230840  5023536   4663524  52% /

watch: read error: No change in terminal output.

Conclusion:

The ‘watch’ command is a versatile and powerful tool for monitoring various aspects of your system, providing real-time tracking and highlighting of changes. Whether you’re keeping an eye on directory files, tracking disk space usage, watching specific processes, or waiting for a particular system change, ‘watch’ offers a straightforward solution to enhance your system oversight. With options to customize frequency and response to changes, it empowers users to stay informed and react promptly to dynamic environments.

Related Posts

How to Use the Aura Package Manager (with Examples)

How to Use the Aura Package Manager (with Examples)

Aura is a feature-rich package manager designed for Arch Linux and the Arch User Repository (AUR).

Read More
Discovering Subdomains with 'amass enum' (with examples)

Discovering Subdomains with 'amass enum' (with examples)

The amass enum command is an integral part of the OWASP Amass project, designed to conduct in-depth DNS enumeration.

Read More
How to use the command 'finger' (with examples)

How to use the command 'finger' (with examples)

The finger command is a utility in Unix-like operating systems that provides users with information about other system users.

Read More