watch

watch

The watch command in Unix and Linux is used to execute a command periodically, displaying its output and updating it on the terminal screen at regular intervals. It's particularly useful for monitoring changes or updates in command output over time without manually running the command repeatedly.

Basic Usage

The basic syntax for the watch command is:

watch [options] command
  • options: Optional flags to modify the behavior of watch.

  • command: The command or script whose output you want to watch.

Examples

Monitoring System Processes

To monitor system processes with ps command updates every 2 seconds:

watch -n 2 'ps aux | grep process_name'
  • -n 2: Specifies the interval (in seconds) at which watch refreshes and re-runs the command (ps aux | grep process_name in this case).

Monitoring Disk Usage

To monitor disk usage with df command updates every 5 seconds:

watch -n 5 df -h
  • df -h: Displays disk space usage in a human-readable format (-h for human-readable).

Monitoring File Changes

To monitor changes in a directory with ls command updates every 3 seconds:

watch -n 3 'ls -l /path/to/directory'
  • ls -l /path/to/directory: Lists detailed information about files and directories in the specified directory.

Options

-n seconds

  • Specifies the update interval in seconds (seconds).

-d

  • Highlights the differences between successive updates. Useful for spotting changes in command output.

-t

  • Removes the header from the watch output, showing only the command output.

-c

  • Clears the screen before running the command, providing a cleaner output.

Practical Use Cases

Monitoring Logs

Use watch to monitor log files or streams for real-time updates, useful for debugging or monitoring applications.

Continuous Integration and Testing

In automated testing or continuous integration environments, watch can monitor test results or build processes, providing immediate feedback on changes.

System Resource Monitoring

Use watch to monitor CPU, memory, or disk usage over time, aiding in system performance analysis and troubleshooting.

Summary

The watch command is a versatile tool for monitoring command output in real-time, allowing you to observe changes or updates at specified intervals. It's useful for a wide range of tasks, including system monitoring, log file analysis, and process management. Understanding its usage and options can enhance your ability to monitor and manage Unix and Linux systems effectively.

help

Last updated