Sometimes, you need to repeatedly run a command or a program to monitor various aspects of your system, like ongoing processes, disk space usage, or user logins. The Linux watch command lets you automate these tasks without having to manually run them again and again.

Using this command, you may monitor system activities in real time such as user logins, network status, memory and CPU usage, disk space, etc. Let’s see how to use the watch command in Linux.

watch output of ls command

What Is the Watch Command?

The watch Linux command runs a specified command or a program repeatedly, at regular intervals, and displays its output in the terminal. This lets you observe the changes in the output in real time. It refreshes the output at each interval, overwriting the command’s previous output. By default, the command repeats every two seconds and continues until you manually stop it usingCtrl + C.

Watch Command Syntax

The syntax of the watch command is:

There are various command line options available to use with the watch command. If you run the watch command without any options, it will run the specified command after every two seconds. For instance, the following will show you the output of the ls command:

The output will show you the changes in the directory listing if a file is created, deleted, or changes in size, while refreshing the output every two seconds.

change update interval of watch command

Run a Command Every X Seconds Using watch

you may change the update interval of the watch command. This means you can tell the watch command to wait for x seconds before repeating the command. To change the update interval, use the-noption followed by the time interval in seconds.

For instance, to run a command after every 5 seconds, run:

Highlight Changes Between Updates

The watch command overwrites its output on each refresh. Using the-doption, you can also highlight the changes between the previous and updated output.

Hide Header in Output of the watch Command

By default, the watch command displays a header at the top of every output that contains the update interval, command name, and the system’s current date and time. You can strip the header from the output using the-toption:

Play Beeps on Error

When a Linux process finishes running, it returns an exit code. By convention, the value is 0 on success and non-zero to represent an error. The watch command’s-boption plays a beep sound if the command returns a non-zero exit.

Let’s say you want to monitor sshd.service and get notified when the service stops; you would use:

This command will give beep sounds when the service stops. The beep will stop when the service resumes. To test this, open another terminal window and stop the service. This will give constant beeps until you start the service again. However, to use this feature, you must have the beep package installed on your system.

Exit the watch Command on Output Change

You can also tell the watch command to stop running and exit when the output of the command changes. You can do this using the-goption. This option is very useful in scenarios when you are waiting for certain changes in the output.

Once the output changes, the watch command will stop. You can combine this usage with the echo command to display a message on the screen.

For example, consider a scenario where you are waiting for a file to arrive in your directory. The following command will monitor the directory for the specified file. As soon as it arrives, the watch command will stop running and the messagefile arrivedwill show on the terminal.

Another useful example is a notification when a specific user logs in to your system:

This command will monitor the output of thewho command which lists the users who are logged in. Once the user logs in, the watch command will stop and display a message on the terminal.

Similarly, you can tell the watch command to stop a service when some changes are made in a file. Keep in mind that when using a piped chain of commands, you’ll need to enclose the entire command within quotes.

Watch Activities in Your Linux System

If you find yourself doing the same process repeatedly, consider using the watch command to automate it. It saves you from frequently typing the commands to see what has changed.

You can also use the watch command with other Linux commands to monitor system resources and activities, and track changes in files or directories.