Linux Guide | How to Install and Use ‘watch’ Command

Terminal interface illustrating the installation of watch used for periodically running commands

Have you ever wanted to keep an eye on changes to a command’s output in real-time? If so, the 'watch'command in Linux can help, allowing you to observe changes to command outputs and processes as they happen. However, installing and using Linux commands can sometimes seem daunting, but fear not, the 'watch' command is a tool that’s well worth getting to know. It’s available on most package management systems, making the installation process straightforward once you understand the steps.

In this guide, we will navigate you through the process of installing the ‘watch’ command on your Linux system. We will provide you with installation instructions for both APT-based distributions like Debian and Ubuntu, and YUM-based distributions like CentOS and AlmaLinux. We’ll also delve into more advanced topics, such as compiling the ‘watch’ command from source and installing a specific version. Finally, we’ll wrap up with guidance on how to use the ‘watch’ command and verify that the correct version is installed.

So, let’s get started and master the ‘watch’ command in Linux!

TL;DR: How Do I Install and Use the ‘watch’ Command in Linux?

In most Linux distributions, the 'watch' command comes pre-installed. You can verify this with, watch -v. However, if it isn’t installed to your system, you can add it via the procps package with the commands: sudo yum install procps-ng or sudo apt install procps. To use it, you can run the command watch [command], which will execute the specified command every 2 seconds by default and display the output.

For example:

watch date

# Output:
# Every 2.0s: date
# Tue Sep 14 12:00:00 UTC 2021

This command will display the current date and time, updating every 2 seconds.

This is a basic way to use the ‘watch’ command in Linux, but there’s much more to learn about this powerful tool. Continue reading for more detailed information and advanced usage scenarios.

Understanding the ‘watch’ Command in Linux

The ‘watch’ command in Linux is a simple yet powerful tool that allows you to monitor changes to a command’s output in real-time. It’s like having a vigilant guard that keeps an eye on a particular process or command and alerts you whenever something changes.

Why would someone want to use it? Well, the ‘watch’ command is particularly useful when you’re waiting for a certain condition to be met. For example, you might be waiting for a large file to finish downloading, a specific process to finish running, or for a change to occur in a directory or file.

Now that we understand what the ‘watch’ command is and why it’s useful, let’s move on to installing it.

Installing the ‘watch’ Command with APT

If you’re using a Debian-based distribution like Ubuntu, you can install the ‘watch’ command using the APT package manager. Here’s how:

sudo apt update
sudo apt install procps

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# procps is already the newest version (2:3.3.12-3ubuntu1.2).
# 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

In this code block, we first update the package lists for upgrades and new packages with sudo apt update. Next, we install the ‘procps’ package, which includes the ‘watch’ command, with sudo apt install procps. The output informs us that ‘procps’ is already the newest version, which means the ‘watch’ command is installed and ready to use.

Installing the ‘watch’ Command with YUM

If you’re using a CentOS-based distribution, you can use the YUM package manager to install the ‘watch’ command. Here’s how:

sudo yum install procps-ng

# Output:
# Loaded plugins: fastestmirror
# Loading mirror speeds from cached hostfile
# Package procps-ng-3.3.10-17.el7.x86_64 already installed and latest version
# Nothing to do

In this code block, we use sudo yum install procps-ng to install the ‘procps-ng’ package, which includes the ‘watch’ command. The output tells us that ‘procps-ng’ is already installed and the latest version, so the ‘watch’ command is installed and ready to use.

Note: The ‘watch’ command is part of the ‘procps’ or ‘procps-ng’ package, which comes pre-installed on most Linux distributions. So, you might already have the ‘watch’ command installed on your system. You can check if it’s installed by running watch -v.

In the next section, we’ll dive into more advanced installation methods and basic uses of the ‘watch’ command, such as changing the interval of command execution and using it with pipes. Stay tuned!

Installing the ‘watch’ Command from Source Code

If you prefer to build your tools from source or need a specific version of the ‘watch’ command, you can compile it from the source code. Here’s how you can do it:

First, download the source code for the ‘procps’ package, which includes the ‘watch’ command, from the official source. You can use the ‘wget’ command to download the source code.

wget http://procps.sourceforge.net/procps-3.2.7.tar.gz

Next, extract the downloaded file with the ‘tar’ command:

tar xvf procps-3.2.7.tar.gz

Navigate to the extracted directory and compile the source code using ‘make’ and ‘make install’:

cd procps-3.2.7
make
sudo make install

Now, you have installed the ‘watch’ command from the source code.

Installing Different Versions of the ‘watch’ Command

From Source Code

To install a specific version of the ‘watch’ command from the source code, you need to download the source code for that specific version. The steps are similar to those described above, but you need to replace the URL in the ‘wget’ command with the URL for the specific version you want to install.

Using Package Managers

APT

On Debian-based distributions, you can use the APT package manager to install a specific version of a package. Here’s how you can do it:

sudo apt install procps=version

Replace ‘version’ with the version number of the ‘procps’ package you want to install.

YUM

On CentOS-based distributions, you can use the YUM package manager to install a specific version of a package. Here’s how you can do it:

sudo yum install procps-ng-version

Replace ‘version’ with the version number of the ‘procps-ng’ package you want to install.

Version Comparison

Different versions of the ‘watch’ command may have different features or may be compatible with different versions of Linux distributions. Here’s a comparison of some major versions:

VersionKey ChangesCompatibility
3.2.7Initial releaseAll
3.2.8Added color supportAll
3.3.10Added support for pipesAll

Using the ‘watch’ Command and Verifying Installation

How to Use the ‘watch’ Command

The ‘watch’ command is very simple to use. The basic syntax is watch [options] command, where ‘command’ is the command you want to monitor.

For example, to monitor the contents of a directory, you can use the ‘watch’ command with the ‘ls’ command:

watch ls /path/to/directory

This command will display the contents of the specified directory and update the display every 2 seconds.

Verifying the ‘watch’ Command Installation

To verify that the ‘watch’ command is installed correctly, you can use the ‘-v’ option, which displays the version information:

watch -v

# Output:
# watch from procps-ng 3.3.10

The output tells you the version of the ‘watch’ command, which confirms that it’s installed correctly.

Exploring Alternatives to ‘watch’ Command

While the ‘watch’ command is a powerful tool for monitoring changes in real-time, Linux offers other commands that can achieve similar results. Let’s explore some of these alternatives and discuss their advantages and disadvantages.

Using ‘tail -f’ Command

The ‘tail -f’ command is commonly used to monitor changes to files in real-time. It’s particularly useful for monitoring log files. Here’s an example:

tail -f /var/log/syslog

# Output:
# Sep 14 13:00:00 localhost CRON[12345]: (root) CMD (command)
# Sep 14 13:00:00 localhost anacron[12345]: Job 'cron.daily' started
# Sep 14 13:00:00 localhost anacron[12345]: Job 'cron.weekly' started

In this example, the ‘tail -f’ command is used to monitor the system log file (/var/log/syslog). The output is updated in real-time as new entries are added to the log file.

However, unlike the ‘watch’ command, ‘tail -f’ can only monitor files, not other commands or processes.

Using ‘top’ Command

The ‘top’ command is another alternative to the ‘watch’ command. It provides a dynamic real-time view of the running system. It can display system summary information and a list of processes currently being managed by the Linux kernel.

Here’s an example:

top

# Output:
# top - 13:00:00 up 10 days,  3:00,  1 user,  load average: 0.00, 0.01, 0.05
# Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
# %Cpu(s):  0.7 us,  0.3 sy,  0.0 ni, 98.9 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
# KiB Mem :  2046740 total,   169420 free,   811832 used,  1164488 buff/cache
# KiB Swap:  2097148 total,  2097148 free,        0 used.  1164488 avail Mem

The ‘top’ command displays a lot of information, including system uptime, number of users, load averages, total tasks, CPU usage, memory usage, and more. It’s a powerful tool for system administrators, but it might be overwhelming for beginners.

In conclusion, while the ‘watch’ command is a powerful tool for monitoring changes in real-time, there are other commands like ‘tail -f’ and ‘top’ that offer different features and can be used as alternatives depending on your specific needs. It’s always a good idea to familiarize yourself with these commands and understand their advantages and disadvantages to choose the best tool for the job.

Troubleshooting Common ‘watch’ Command Issues

As with any command, you may encounter some issues when using the ‘watch’ command. Here are some common problems and their solutions.

Command Not Found Error

If you encounter a ‘command not found’ error when trying to use the ‘watch’ command, it’s likely that the ‘watch’ command is not installed on your system. You can install it using your distribution’s package manager. For example, on Ubuntu, you can use the following commands:

sudo apt update
sudo apt install procps

# Output:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# procps is already the newest version (2:3.3.12-3ubuntu1.2).
# 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

This will install the ‘procps’ package, which includes the ‘watch’ command.

Permission Denied Error

If you encounter a ‘permission denied’ error when trying to use the ‘watch’ command, it’s likely that you don’t have the necessary permissions to execute the command you’re trying to monitor. You can solve this by using the ‘sudo’ command to run the command as the root user. For example:

sudo watch ls /path/to/directory

This will run the ‘watch’ command as the root user, which should bypass the ‘permission denied’ error.

Command Doesn’t Update

If the command you’re monitoring with the ‘watch’ command doesn’t seem to be updating, it’s possible that the command is cached. The ‘watch’ command uses the system’s cache by default, which can sometimes lead to stale results. You can solve this by using the ‘-n’ option with the ‘watch’ command to disable caching. For example:

watch -n ls /path/to/directory

This will disable caching for the ‘watch’ command, ensuring that you always get fresh results.

Remember, the ‘watch’ command is a powerful tool, but it’s not always the best solution for every situation. Depending on your specific needs, other commands like ‘tail -f’ or ‘top’ might be more appropriate. Always consider your options and choose the best tool for the job.

The Fundamentals of Command Execution and Output Monitoring

Before we delve further into the ‘watch’ command, let’s take a step back and understand the basics of command execution and output monitoring in Linux. This will provide us with a solid foundation to comprehend the underlying concepts of the ‘watch’ command.

Command Execution in Linux

In Linux, commands are executed by the shell, which is a program that interprets and executes commands read from the command line or a script. When you type a command and press Enter, the shell interprets the command, performs any necessary redirections, and then executes the command.

For example, when you type ls /home, the shell interprets the ‘ls’ command and the ‘/home’ argument, and then lists the contents of the /home directory.

ls /home

# Output:
# user1  user2  user3

In this example, the ‘ls’ command lists the users in the /home directory. The shell interprets the command and executes it, displaying the output on the terminal.

Real-Time Output Monitoring in Linux

Real-time output monitoring is an essential aspect of system administration in Linux. It allows you to monitor the output of a command or the contents of a file as they change in real time. This can be particularly useful when you’re monitoring log files, system resources, or the progress of a command.

For example, you can use the ‘top’ command to monitor system resources in real time:

top

# Output:
# top - 13:00:00 up 10 days,  3:00,  1 user,  load average: 0.00, 0.01, 0.05
# Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
# %Cpu(s):  0.7 us,  0.3 sy,  0.0 ni, 98.9 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
# KiB Mem :  2046740 total,   169420 free,   811832 used,  1164488 buff/cache
# KiB Swap:  2097148 total,  2097148 free,        0 used.  1164488 avail Mem

In this example, the ‘top’ command provides a dynamic real-time view of the system resources, including CPU usage, memory usage, and more.

The ‘watch’ command in Linux extends this concept of real-time monitoring by allowing you to monitor the output of any command in real time. This makes it a versatile and powerful tool for system administration and troubleshooting in Linux.

The Relevance of Real-Time Monitoring in System Administration

Real-time monitoring is a critical aspect of system administration. It allows system administrators to keep a pulse on their systems, identify issues before they become critical, and respond to changing conditions in real time. The ‘watch’ command in Linux is a powerful tool that can aid in real-time monitoring by allowing system administrators to monitor the output of any command as it changes.

For example, you might use the ‘watch’ command to monitor the disk usage of a particular directory in real time:

watch du -sh /path/to/directory

# Output:
# Every 2.0s: du -sh /path/to/directory
# 1.2G    /path/to/directory

In this example, the ‘watch’ command is used to monitor the disk usage of the specified directory. The output is updated every 2 seconds, providing real-time feedback on the disk usage.

Exploring Related Concepts: Log Monitoring and Process Management

Beyond the ‘watch’ command, there are other related concepts that are worth exploring to gain a deeper understanding of system administration in Linux.

Log monitoring is a critical aspect of system administration. It allows system administrators to keep track of system events and identify issues. Linux provides various tools for log monitoring, such as ‘logwatch’ and ‘syslog’.

Process management is another important aspect of system administration. It involves starting, stopping, and monitoring processes. Linux provides various tools for process management, such as ‘ps’, ‘top’, and ‘htop’.

For example, you might use the ‘ps’ command to monitor the status of a particular process:

ps aux | grep process_name

# Output:
# user1  12345  0.0  0.1 123456 12345 ?        S    13:00   0:00 process_name

In this example, the ‘ps’ command is used to display information about the specified process. The output includes the user who owns the process, the process ID, the CPU and memory usage, and more.

Further Resources for Mastering Linux System Administration

To deepen your understanding of real-time monitoring, log monitoring, and process management in Linux, consider exploring these resources:

  1. The Linux Documentation Project: This is an extensive resource for all things Linux, including detailed guides on various aspects of system administration.

  2. Linux Journal: An online magazine dedicated to Linux, it offers articles, tutorials, and tips for Linux users.

  3. Unixmen: This website provides tutorials, how-tos, and articles on Linux and Unix-like operating systems, including system administration topics.

These resources will provide you with a wealth of information and practical guidance to help you master system administration in Linux.

Wrapping Up: Installing the ‘watch’ Command in Linux

In this comprehensive guide, we’ve dived deep into the world of the ‘watch’ command in Linux, a powerful tool for monitoring real-time changes in command outputs and processes.

We embarked on this journey with the basics, learning how to install and use the ‘watch’ command. We then delved into more advanced uses, exploring how to change the interval of command execution and use the ‘watch’ command with pipes. Along the way, we tackled common challenges you might encounter when using the ‘watch’ command, providing you with solutions and workarounds for each issue.

We also explored alternative approaches to real-time monitoring, such as using ‘tail -f’ or ‘top’, giving you a sense of the broader landscape of tools available in Linux for this purpose. Here’s a quick comparison of these methods:

MethodProsCons
‘watch’ CommandMonitors any command’s outputMay require troubleshooting for certain situations
‘tail -f’ CommandIdeal for monitoring log filesCan only monitor files
‘top’ CommandProvides a dynamic real-time view of the systemMay be overwhelming for beginners

Whether you’re a beginner just starting out with the ‘watch’ command or a seasoned system administrator looking for a refresher, we hope this guide has served as a useful resource.

The ‘watch’ command in Linux offers a powerful means of keeping a pulse on your system’s activities. With this knowledge, you’re well-equipped to leverage its capabilities for efficient system administration. Happy monitoring!