2024-05-27
watch
CommandThe 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).
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:
watch
: Invokes the watch
command.-n 2
: Specifies an update interval of 2 seconds. The default is 2 seconds.top -bn1
: Executes the top
command. -b
runs top
in batch mode, and -n 1
tells it to only run a single iteration. This is for watch
; otherwise, top
would constantly scroll and be unreadable.head -n 15
: Takes only the top 15 lines of top
’s output to prevent the display from becoming overwhelmingly large.This will display a continuously updating view of the top 5 processes, updated every 2 seconds.
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.
watch
with Other Process Management Commandswatch
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.
watch
The watch
command offers many options to refine monitoring:
-d
: Highlights changes between updates. This is very helpful for spotting fluctuations.-c
: Clears the screen before each update, offering cleaner output.-g
: Uses less
to view the output, enabling scrolling through past updates.-t
: Suppresses the header that shows the command and elapsed time.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.
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:
ps aux
.grep
.awk
.sort -k3 -nr
).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.