Posted on
Software

watch: Repeat commands at set intervals

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering the watch Command: Your Tool for Automating Repeated Tasks in Linux

In the world of Linux, efficiency and automation are not just beneficial; they're a way of life. One of the less celebrated but incredibly powerful tools in the Linux arsenal is the watch command. This simple yet robust utility allows you to run other commands at regular intervals, thus automating the continuous monitoring of files, processes, or any other tasks that change over time. Let's explore how you can leverage watch to supercharge your productivity, along with how to install it on various Linux distributions.

What is the watch Command?

The watch command in Linux is used primarily for running a program periodically, showing output in fullscreen. It can be immensely useful for administrators and developers who need to continually monitor the health of processes or keep an eye on system outputs and resources. Essentially, it executes any specified command at regular intervals and displays the output in real time.

Installing watch on Different Linux Distributions

Before diving into how to use watch, it's essential to ensure you have it installed on your system. watch is available in the official repositories of most Linux distributions. Here’s how to install it:

Debian/Ubuntu (Using apt):

On distributions like Debian or Ubuntu, use the apt package manager:

sudo apt update
sudo apt install procps

The watch command is part of the procps package, which is a suite of command-line utilities for monitoring processes.

Fedora (Using dnf):

For Fedora and other similar distributions that use dnf:

sudo dnf install procps-ng

Here, the package containing watch is procps-ng.

openSUSE (Using zypper):

In openSUSE, you can use the zypper package manager:

sudo zypper install procps

This command installs the procps package, which includes watch.

Using the watch Command

Now that you have watch installed, using it is straightforward. The basic syntax of watch is:

watch [options] command

Here are a few practical examples of how watch can be utilized:

  1. Monitoring Disk Space:

    You can use watch to continuously monitor disk usage:

    watch df -h
    

    This command refreshes the output of df -h every 2 seconds by default, helping you keep an eye on the disk space.

  2. Tracking Log File Updates:

    If you want to observe changes in a log file real-time, you can use:

    watch tail -n 20 /var/log/syslog
    

    This is especially useful for monitoring logs during troubleshooting or system assessment.

  3. Checking System Health:

    To monitor the top processes affecting your system’s CPU and memory, you might regularly run:

    watch -n 5 top -b -n 1 | head -20
    

    This updates the list of top processes every 5 seconds.

Options You Might Find Useful

  • -n: Specifies the interval between command executions in seconds.

  • -d: Highlights differences between consecutive updates.

  • -b: Beep if command has a non-zero exit.

Conclusion

The watch command is a phenomenal tool for anyone who needs to automate the periodic execution of Linux commands. Whether you’re a system admin wanting to monitor system metrics, a developer keeping an eye on an application, or a Linux enthusiast curious about real-time data processing, watch provides a simple yet powerful solution.

Keep experimenting with different commands and watch options to fully harness its capabilities and customise it to your needs. With watch, the power of automation is just a command away!