How to Use the watch Command on Linux to Run Programs Periodically

35 min read

Sometimes, you need to repeatedly run commands or programs to monitor various aspects of the system, such as running processes, disk space usage, or user logins. The watch command in Linux allows you to automate these tasks without manually running them over and over again.

With this command, you can monitor system activities in real-time, such as user logins, network status, memory and CPU usage, disk space, and more. Let's see how to use the watch command in Linux.

What is the watch Command?

The watch command in Linux repeatedly runs a specified command or program at fixed intervals and displays its output in the terminal. This allows you to observe changes in the output in real-time. It refreshes the output at each interval, overwriting the previous output. By default, the command repeats every two seconds until you manually stop it with Ctrl + C.

Syntax of the watch Command

The syntax for the watch command is as follows:

watch [options] <command>

There are various command-line options available for the watch command. If you run the watch command without any options, it will run the specified command every two seconds. For example, the following command will display the output of the ls command:

watch ls -l ~/

The output will show changes in the directory listing, refreshing every two seconds if files are created, deleted, or their sizes change.

Run a Command Every X Seconds with watch

You can change the update interval of the watch command. This means you can tell the watch command to wait X seconds before repeating the command. To change the update interval, use the -n option followed by the time interval in seconds.

For example, to run a command every 5 seconds, run:

watch -n 5 <command>

Highlight Changes Between Updates

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

watch -d <command>

Hide the Header in watch Command Output

By default, the watch command displays a header at the top of each output, including the update interval, command name, and the current date and time of the system. You can remove the header from the output using the -t option:

watch -t <command>

Play a Beep Sound on Error

When a Linux process finishes running, it returns an exit code. By convention, this value is 0 for success and non-zero for errors. The -b option of the watch command will play a beep sound when the command returns a non-zero exit code.

For example, suppose you want to monitor the sshd.service and be notified when the service stops. You can use:

watch -b sudo systemctl status sshd.service

This command will beep when the service stops. The beep will stop when the service resumes running. 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 achieve this with the -g option. This option is useful when 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 appear in your directory. The following command will monitor the specified file's directory. Once the file appears, the watch command will stop running, and the message "file arrived" will be displayed on the terminal.

watch -g "ls -l | grep filename" && echo "file arrived"

Another useful example is to be notified when a specific user logs into the system:

watch -g "who | grep username" && echo "username logged in"

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

Similarly, you can tell the watch command to stop the service when changes occur in a file. Note that when using a chain of commands with pipes, you need to enclose the entire command in quotes.

Monitor Activities on a Linux System

If you find yourself repeatedly performing the same process, consider using the watch command to automate it. This can save you from frequently typing commands to see what has changed.

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