watch is an excellent command which can run scripts or commands at a regular intervals or repeatedly. This is very much useful when we want to see a command in continuously running phase similar to top command. This is very much useful when troubleshooting an issue and requires to monitor a command output continuously. If we consider top command it will continuously check the process states and memory details for every three seconds by default. We will see some useful examples of watch command in this post.
Linux watch command syntax
watch [options] linux-command
Learn watch command with examples
Example1: Check the progress of cp command. By default cp command will not show any progress when coping data from one location to other. We can use watch command along with ls command or du command on the destination folder to see the progress.
watch du -s /tmp/CentOS-6.2-i386-bin-DVD1.iso
watch ls -s /tmp/CentOS-6.2-i386-bin-DVD1.iso
Note: To exit from watch command we have to press Ctrl+c to cancel or stop the process.
We created a screen cast and recorded below gif image for seeing the progress.
Example2: Dont have iptraf or vnstat commands and want to observe bandwidth stats, use watch command with ifconfig command.
Example3: By default watch command runs a command or script for every two seconds by default, we can increase or decrease the interval by using -n option
Run ls command for every 1 second
watch -n 1 ls -l
Run free command for every 1/10 second
watch -n 0.1 free
Note: The minimum time watch can take is 0.1 second and not less than that. If you want to run a command less than that time we can create a daemon script using while loop. And if you want to run a command for every one hour or so we can use Linux scheduling "crontab". We can not view command output, if we run commands in crontab.
Example 4: Display changes in output of a command with -d option of watch command. Some times it’s required to see the difference between the previous output and present output with some color difference, we can use this option.
watch -d -n 1 free
Example 5: Dont want to see time ticking at the start at watch screen, we can use -t to suppress it.
watch -t free
Hope this helps to troubleshoot issues in bit meaning ful way.
Latest posts by Surendra Anne (see all)
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018