How to run a program periodically using the Watch command on Linux
Sometimes you need to continuously run a command or program to monitor various aspects of your system, such as ongoing processes, disk space usage, or user login information. The watch command on Linux allows you to automate these tasks without having to run them manually over and over again.
Using the watch command, you can monitor real-time system activities such as user login information, network status, memory and CPU usage, disk space, etc. See how to use the watch command in Linux.
What is the watch command?
The watch Linux command runs a specified command or program repeatedly, at regular intervals, and displays the output of that command in Terminal. This allows you to observe changes in 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 with Ctrl + C .
Watch command syntax
The syntax of the watch command is:
There are many different command line options available for use with 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 will show you the output of the ls command:
watch ls -l ~/
The output will show you changes in the directory listing if a file is created, deleted or resized, and refreshes the output every two seconds.
Run command every X seconds using 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 interval in seconds.
For example, to run a command every 5 seconds, run:
watch -n 5
Highlight changes between updates
The watch command overwrites its output on each refresh. Using the -d option , you can also highlight changes between the previous output and the updated output.
Hide headers in the output of the watch command
By default, the view command displays a header at the top of each output containing the update interval, command name, and the current system date and time. You can remove headers from the output with the -t option :
Beep when there is an error
When a Linux process finishes running, it returns an exit code. By convention, the value is 0 for success and non-zero to indicate error. The -b option of the watch command will beep if the command returns a non-zero exit result.
Suppose you want to monitor sshd.service and receive notifications when the service stops; you will use:
watch -b sudo systemctl status sshd.service
This command will beep when the service stops. The beeping will stop when service resumes. To test this, open another terminal window and stop the service. This will beep continuously until you start the service again. However, to use this feature, you must install the beep package on your system.
Exit the watch command when the output changes
You can also tell the watch command to stop running and exit when the command's output changes. You can do this using the -g option . This option is useful in situations when you are expecting certain changes in the output.
When 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 situation in which 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 file arrived message will display on Terminal.
watch -g "ls -l | grep filename" && echo "file arrived"
Another useful example is notification when a particular user logs into your system:
watch -g "who | grep username" && echo "username logged in"
This command will monitor the output of the who command listing logged in users. After the user logs in, the watch command will stop and display a notification on Terminal.
Similarly, you can tell the watch command to stop the service when some changes are made in a file. Remember that when using a command string in a path, you will need to enclose the entire command in quotes.
- How to use the which command in Linux
- 20+ essential Linux security commands
- How to use the stat command on Linux
- 14 interesting Linux commands in Terminal
- Basic Linux commands everyone needs to know
- The Cat command in Linux
- How to use the last command in Linux
- How to use ss command on Linux
- Filter and Pipe in Unix / Linux
- 11 uses of ps command in Linux