Linux watch Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux watch command, which allows you to repeatedly execute a command and monitor its output. The lab covers understanding the watch command, monitoring system processes, and tracking file changes in real-time. The watch command is a powerful tool for system monitoring and management, enabling you to stay informed about critical system events and changes. Throughout the lab, you will explore practical examples and learn how to effectively utilize the watch command in your daily system administration tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/SystemInformationandMonitoringGroup -.-> linux/watch("`Command Repeating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") subgraph Lab Skills linux/cat -.-> lab-423002{{"`Linux watch Command with Practical Examples`"}} linux/watch -.-> lab-423002{{"`Linux watch Command with Practical Examples`"}} linux/ls -.-> lab-423002{{"`Linux watch Command with Practical Examples`"}} linux/ps -.-> lab-423002{{"`Linux watch Command with Practical Examples`"}} linux/top -.-> lab-423002{{"`Linux watch Command with Practical Examples`"}} linux/df -.-> lab-423002{{"`Linux watch Command with Practical Examples`"}} end

Understand the watch Command

In this step, you will learn about the watch command in Linux, which allows you to repeatedly execute a command and monitor its output.

The watch command is a powerful tool for system monitoring and management. It can be used to track changes in system processes, file modifications, and other real-time events.

To start using the watch command, simply run the following in your terminal:

watch [options] <command>

Here, [options] are the optional parameters you can pass to the watch command, and <command> is the command you want to execute and monitor.

Some common options for the watch command include:

  • -n, --interval <seconds>: Set the update interval for the command execution (default is 2 seconds).
  • -d, --difference: Highlight the differences between successive updates.
  • -t, --no-title: Hide the header showing the current time and the command being executed.

For example, to monitor the output of the df command (which shows the disk usage) every 5 seconds, you can run:

watch -n 5 df -h

Example output:

Every 5.0s: df -h                                                   labex@ubuntu: Fri Apr 28 14:35:41 2023

Filesystem      Size  Used Avail Use% Mounted on
overlay         30G  4.2G   26G  14% /
tmpfs           64M     0   64M   0% /dev
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1       30G  4.2G   26G  14% /etc/hosts
shm              64M     0   64M   0% /dev/shm
overlay         30G  4.2G   26G  14% /etc/resolv.conf
overlay         30G  4.2G   26G  14% /etc/hostname
overlay         30G  4.2G   26G  14% /etc/host.conf
tmpfs           2.0G     0  2.0G   0% /proc/acpi
tmpfs           2.0G     0  2.0G   0% /proc/scsi
tmpfs           2.0G     0  2.0G   0% /sys/firmware

The watch command will continuously execute the df -h command and display the output, updating it every 5 seconds.

Monitor System Processes with watch

In this step, you will learn how to use the watch command to monitor system processes in real-time.

The watch command can be used in conjunction with the ps (process status) command to continuously display information about running processes on your system.

To monitor system processes using watch, run the following command:

watch -n 5 'ps aux'

This will execute the ps aux command every 5 seconds and display the output in your terminal. The ps aux command shows information about all running processes, including the user, process ID, CPU and memory usage, and the command that started the process.

You can also use the watch command with other process-related commands, such as top or htop, to get a more detailed view of system processes:

watch -n 5 top

This will display the top command output, which shows the processes sorted by CPU or memory usage.

Example output:

Every 5.0s: top                                                     labex@ubuntu: Fri Apr 28 14:40:41 2023

top - 14:40:41 up 8 min,  0 users,  load average: 0.00, 0.00, 0.00
Tasks:  38 total,   1 running,  37 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    1984.0 total,    1909.7 free,      29.3 used,      45.0 buff/cache
MiB Swap:       0.0 total,       0.0 free,       0.0 used.    1909.9 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      1 root      20   0    8548   3292   2340 S   0.0   0.2   0:01.38 systemd
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp

The watch command allows you to continuously monitor system processes, making it easier to identify resource-intensive applications or detect any unusual process activity.

Monitor File Changes with watch

In this step, you will learn how to use the watch command to monitor changes in files or directories.

The watch command can be used in conjunction with the ls (list directory contents) command to continuously display the contents of a directory and detect any changes.

First, let's create a directory and some files to monitor:

mkdir ~/project/files
touch ~/project/files/file1.txt ~/project/files/file2.txt

Now, to monitor the contents of the ~/project/files directory, run the following command:

watch -n 5 'ls -l ~/project/files'

This will execute the ls -l ~/project/files command every 5 seconds and display the output in your terminal. The -l option to ls shows the long-format listing, which includes information such as file permissions, ownership, size, and modification time.

You can also use the watch command to monitor changes in a specific file. For example, to monitor the contents of the file1.txt file:

watch -n 5 'cat ~/project/files/file1.txt'

This will display the contents of the file1.txt file every 5 seconds, and you'll be able to see any changes made to the file in real-time.

Example output:

Every 5.0s: ls -l ~/project/files                                    labex@ubuntu: Fri Apr 28 14:45:41 2023

total 0
-rw-r--r-- 1 labex labex 0 Apr 28 14:44 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 28 14:44 file2.txt

The watch command allows you to continuously monitor files and directories, making it easier to detect changes or modifications to your system's files.

Summary

In this lab, you will learn how to use the watch command in Linux to monitor system processes and file changes. First, you will understand the basics of the watch command, including how to run it and the available options. Then, you will learn how to use watch to monitor system processes, such as tracking changes in disk usage over time. Finally, you will explore how to use watch to monitor file changes, which can be useful for tracking configuration files or log files. By the end of this lab, you will have a solid understanding of the watch command and how to apply it to various system monitoring tasks.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like