6 Simple Examples of Using the Linux watch Command

Try this guide with our instant dedicated server for as low as 40 Euros

linux watch command

Whenever you need to execute a specific command at predefined intervals in a Linux environment, you may use cron jobs or bash scripts. 

These options are great at what they do – automate the execution of commands at the preset time or intervals.

However, Linux presents a simpler and more interactive alternative: the watch command.

In many cases, this command offers more insights into how the command is executed and how the execution affects the system processes. 

This tutorial will help you understand the Linux watch command. We will cover the basic syntax and then look into some use cases where you can apply the command to accomplish various tasks.

Table Of Contents

  1. An Overview of the Linux Watch Command
    1. The Syntax of the Linux watch Command
    2. The watch Command Options
  2. Examples of Using the Linux Watch Command
    1. The Prerequisites
    2. Example #1: Run a Command with a Specified Interval
    3. Example #2: Highlight Differences Between Updates
    4. Example #3: Exit on Change
    5. Example #4: Hide the watch Command Header
    6. Example #5: Alert on Error Using Beep
    7. Example #6: Use Complex Commands
  3. Conclusion
  4. FAQs

An Overview of the Linux Watch Command

The watch command is a native utility in many Linux distributions. It is designed for executing user-specified commands at set intervals. When it becomes active, it clears the terminal momentarily and displays the output of the designated command. It also shows the current system date and time.

Typically, the watch command refreshes the output every two seconds. To terminate the command output, press Ctrl+C.

Unlike similar commands that execute commands and scripts at predetermined intervals, watch is a great way of monitoring the changing impact of the commands over time. As such, it is a powerful option for tasks like observing disk usage, system uptime, or tracking errors.

Also Read: Get Current Date and Time in Python With 2 Easy Methods

The Syntax of the Linux watch Command

Here is the basic syntax of the watch command in Linux:

# watch [option] [command]

Where:

  • [option]: Modify the behavior of the watch command.
  • [command]: The command you wish to execute repetitively.

The watch Command Options

The watch command offers the following options:

linux watch command options

Also Read: lsof Command in Linux with Examples

Examples of Using the Linux Watch Command

Now that you have a good idea about the watch command in Linux, let’s see it in action. We will now discuss six scenarios where we will use the command. But first, let’s discuss the prerequisites.

The Prerequisites

Here are the prerequisites you need to try out the examples:

  • A system with a mainstream Linux distribution.
  • A user account with root or sudo privileges.

Example #1: Run a Command with a Specified Interval

You can specify a custom interval for executing a user-defined command and displaying the output using the -n or –interval option:

# watch -n [interval in seconds] [command]

For instance, run the following command to print the system’s time and date every 5 seconds:

# watch -n 5 date

watch -n 5 date

Example #2: Highlight Differences Between Updates

The most interesting way of using the watch command is highlighting changes between successive output updates. 

For this, we suggest using the -d or –difference option to highlight changes between successive output updates:

# watch -d [command]

For example, here is the command that displays the system date and time at the default 2-second interval with the changes highlighted:

# watch -d date

watch -d date

To retain highlighting for all values that have ever changed, append =cumulative to the -d option:

# watch -d=cumulative date

Example #3: Exit on Change

The -g or –chgexit option causes the watch command to exit if there is a change in the output:

# watch -g [command]

For instance, using the free command with watch monitors your system’s memory consumption and exits if the value changes:

# watch -g free

watch -g free

Also Read: Master the Linux Free Command: 6 Examples For Sysadmins and DevOps Engineers

Example #4: Hide the watch Command Header

By default, the watch command shows a header that shows the interval time, user-defined command, and current system time in the watch command output. If you wish, you can use the -t or –no-title option to hide the title:

# watch -t [command]

For example, if you want to display the system date and time without the header, you would run:

# watch -t date

watch -t date

Example #5: Alert on Error Using Beep

The watch command employs the beep package to play a sound alert if the output update fails due to an error. To enable this functionality, use the -b or –beep option in the following syntax:

# watch -b [command]

Example #6: Use Complex Commands

The watch command allows for the use of complex user-defined commands, including their arguments and options. This can be achieved by utilizing the backslash (\) symbol in the following syntax.

# watch [options] \

When you run this command syntax, you can enter the arguments and hit Enter. At this point, the command executes. 

Consider the following example:

# watch -n 5 \

echo "watch command example output"

echo watch command example output

Alternatively, you can enclose the user-defined command within single quotation marks:

# watch [options] '[command]'

Using the example above, the command would be:

# watch -n 5 'echo "watch command example output"'

watch command example output

Also Read: Mastering the Linux tee Command for Redirecting Output In 9 Examples

Also Read: A Guide on How to Install and Use Linux Screen Command in 2024

Conclusion

The watch command is a versatile tool in the Linux environment, allowing users to monitor command outputs at regular intervals. By mastering the watch command, users can efficiently track changes in system parameters, monitor resource utilization, and automate repetitive tasks. 

Whether you’re a novice or an experienced Linux user, integrating the watch command into your workflow can greatly enhance your productivity and system management capabilities.

Looking to streamline your system monitoring further on Self Managed servers? Explore RedSwitches, a leading provider of robust server solutions. With our expertise, you can optimize your infrastructure for peak performance and reliability. Discover more at RedSwitches.

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. How can I install the watch command on Linux?

The watch command is often pre-installed on most Linux distributions. However, if it’s not available, you can easily install it using your package manager. For instance, on Debian-based systems like Ubuntu, use sudo apt-get install watch.

Q. Can I adjust the interval for output updates with the watch command?

Yes, you have the flexibility to customize the update interval using the -n or –interval option, followed by the desired time in seconds. For example, watch -n 5 [command] will update every 5 seconds.

Q. How do I terminate the execution of the watch command?

To stop the watch command, simply press Ctrl + C. This action will halt the command’s execution and return you to the shell prompt.

Q. Is it possible to highlight changes between successive output updates?

Absolutely! You can employ the -d or –differences option to highlight discrepancies between successive updates, akin to using diff commands.

Q. Is the watch command suitable for monitoring server resources?

Yes, indeed. The watch command is commonly utilized for real-time monitoring of server resources, including CPU and memory usage, active connections, and network activity. This makes it invaluable for system administrators and operators alike.

Q. Can I display file download progress using the watch command?

Yes, you can monitor file download progress by using pipeline commands with the watch command. For instance, you can use the tail command to track changes in a log file or download progress.

Q. How can I use quotes with the watch command?

You can enclose commands with arguments or options in double quotes or single quotes when using the watch command. This ensures proper execution of complex commands or commands with special characters.

Q. What is the purpose of the watchrun command?

The watchrun command is not a standard Linux command. However, you can achieve similar functionality by combining the watch command with pipeline commands or by creating bash scripts.

Q. What are some common command options available with the watch command?

Some common command options available with the watch command include -n for specifying the update interval, -d for highlighting differences between successive updates, and -t for removing the header displaying interval, command, date, and time.

Q. Can I use the watch command to monitor system uptime?

Yes, you can monitor system uptime by using the uptime command with the watch command. This provides real-time updates on the system’s uptime, displaying successive updates at specified intervals.

Try this guide with our instant dedicated server for as low as 40 Euros