Objectives
Understand the usage of the watch program in Linux.
Prerequisites
- Linux OS Server (preferably CentOS or any other rpm-based distros)
Steps
Step 1: Install Watch Tool
If the watch command does not exist in your Linux OS machine, you can install the procps package.
For rpm-based distros: –
yum install -y procps
For debian-based distros: –
apt-get install procps
Step 2: Understanding Watch
The watch command is an effective tool used to allow commands to be run in a periodical manner. In order words, this is extremely useful if you wish to monitor changes in the results of an executed command in a terminal window.
To use the command to monitor the memory usage, simply type: –
watch free -m
You will be directed to a terminal windows where the free program will be run every 2 seconds by default.
Step 3: Other Useful Options
If you wish to change the number of seconds between updates, use the -n or –internal options to specify a different time frame.
In the example below, we specify the watch command to run the free program every 1 second.
watch -n 1 free -m
OR
watch --interval 1 free -m
You can also highlight the differences between updates by running the -d or –difference option.
In the example below, we use the -d option to note the changes in the system uptime.
watch -d uptime
OR
watch --difference uptime
You can also automatically exit the watch command after a change has been detected with the -g or –chgexit option.
In the example below, the system will exit the watch command after any changes in memory usage is detected.
watch -g free -m
OR
watch --chgexit free -m
To check the version of the watch program, simply use the -v option.
watch -v
To know what other options are available within the watch tool, you can use the -h option to retrieve the user manual.
watch -h