Linux Watch Command, To Monitor a Command Activity
Recently i came to know about watch command, from one of my friend when i have a different requirement. I got good benefit from watch command and i want to share with you people to get more benefit on it, when you have a problem on Linux system.
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 is run 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 command.
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.
Exaample-1 Free command output with watch. Here we can get single output with regular interval.
$ watch free -m
Exaample-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
$ 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.
Exaample-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.
Exaample-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
Exaample-6 Monitor log files in real-time with help of watch command.
$ watch tail /var/log/messages
Read more about watch
If you want to know more option which is available for watch, simply navigate to man page.
# watch --help or # man watch