Watch Command – To monitor the command activity in Linux
Recently I came to know about watch command, from one of my friend when I have different requirements. I felt very useful for me, So I thought of sharing with you people also.
What’s watch command
Watch is a very powerful command-line tool which execute a program or commands or script at a regular intervals or repeatedly or periodically and displays the results on standard output in full screen, similar to top command.
By default, the program runs every 2 seconds. Add
--interval with watch command to specify a different interval, this interval is interpreted as the amount of time between the completion of one run of command and the beginning of the next run. It will not automatically exit, until you terminate it either by
kill the process.
Watch command comes in handy when you want to troubleshoot the issue by monitoring a command output continuously. It will monitor anything, not only commands.
How to install watch in Linux ?
Watch command is available in default repository and installed by default. In case, if it’s not installed, use the distribution package manager and get installed.
[Install watch in Debian/Ubuntu/Mint] $ sudo apt-get install watch [Install watch in RHEL/CentOS] $ sudo yum install watch [Install watch in Fedora] $ sudo dnf install watch [Install watch in openSUSE] $ sudo zypper install watch [Install watch in Arch Linux based system] $ sudo pacman -S watch
Common Syntax :
watch [options] Linux-command
How to use watch command in Linux ?
We are going to add few examples here, for better explanation about watch command, so that you can understand how to use in critical situations.
Free command have a option to get the output at a regular intervals but its not much good compare with top command. When we use Free with watch command, we can get the same kind of results, which will help us to dig the issue in detailed manner.
Example-1 Free command output with watch. Here we can get single output with regular interval.
$ watch free -m
Example-2 By default watch command display the output for every 2 second. Use the following format to run watch command for every 1 second.
$ watch -n 1 free -m
-d option with watch command to get the clear difference between the output (it will highlight previous output and present output).
$ watch -d -n 1 free -m
By default we can’t observe bandwidth stats through ifconfig command. It can be done through combination of watch command.
Example-4 Monitor particular Ethernet bandwidth stats with help of watch command.
$ watch ifconfig
cp command wont have option to show progress when coping data from one location to other. We can see the progress of copy command, when we combine a watch command with ls or du command on destination folder.
Example-5 First run the cp command and use ls or du command with watch to see the copy command progress.
$ sudo cp dump.tar.gz /opt &
Monitor copy command progress.
$ watch -n 0.1 du -h /opt/dump.tar.gz or $ watch -n 0.1 ls -h /opt/dump.tar.gz
Example-6 Monitor log files in real-time with help of watch command.
$ watch tail /var/log/messages
To read more about watch
If you wanna know more option which is available for watch, simply navigate to man page.
# watch --help or # man watch