watch

2024-05-27

Understanding the watch Command

The watch command executes a specified command at regular intervals, displaying the output in a continuously updated window. This makes it ideal for monitoring processes, system load, network activity, and much more. Its primary use case in process management is observing the changes in running processes over time.

The basic syntax is:

watch [options] command

Where command is any command you’d like to monitor, and options allow for customization (we’ll look at these shortly).

Basic Process Monitoring with watch

Let’s start with a simple example: monitoring the top 5 CPU-consuming processes using top.

watch -n 2 top -bn1 | head -n 15

This command does the following:

This will display a continuously updating view of the top 5 processes, updated every 2 seconds.

Monitoring Specific Processes

You might need to focus on particular processes. Using ps in conjunction with grep allows you to achieve this:

watch -n 1 'ps aux | grep "my_process"'

Replace "my_process" with the name of the process you’re interested in. This command monitors all processes containing “my_process” in their command line, updating every second. Remember that grep is case-sensitive, so ensure your process name matches exactly.

Using watch with Other Process Management Commands

watch is flexible and works well with many other process management commands. For instance, you can monitor memory usage of a specific process using pmap:

watch -n 5 'pmap $(pgrep my_process)'

This command will update every 5 seconds, showing the memory map of the process named “my_process”. Remember to replace "my_process" with your target process.

Advanced Options with watch

The watch command offers many options to refine monitoring:

Combining Commands for Powerful Monitoring

The true power of watch lies in its ability to combine commands. You can chain commands together to create detailed process monitoring solutions tailored to your specific needs. For instance, you could combine ps, grep, awk, and sort to track various metrics of a specific process.

Example: Monitoring CPU and Memory Usage of a Specific Process

Let’s create a more advanced example that combines many commands to monitor both CPU and memory usage of a process named “myapp”:

watch -n 2 'ps aux | grep "myapp" | awk "{print \$2, \$3, \$4}" | sort -k3 -nr | head -n 5'

This command does the following:

  1. Lists all processes with ps aux.
  2. Filters for the process “myapp” using grep.
  3. Extracts the PID, %CPU, and %MEM using awk.
  4. Sorts processes by %MEM usage in descending order (sort -k3 -nr).
  5. Displays the top 5 processes using head.

This provides a concise, real-time view of the resource consumption of your target process. Remember to replace "myapp" with your process name. Experiment with different combinations of commands and options to tailor your monitoring to your requirements.