watch runs commands repeatedly – execute a program periodically

In this article we are going  to learn about watch command. Watch runs commands repeatedly. How this watch command is helpful for us..? 

  When you would like to run one command multiple times by giving intervals, watch is ultimate solution for you. watch runs command repeatedly, displaying its output and errors. This allows you to watch the program output change over time. By default, the program is run every 2 seconds. By default, watch will run until interrupted.

Watch runs commands repeatedly

Check with date command as date updates at every second. If you use option -d along with watch command data update will be highlighted.

[root@ArkITShell ~]#watch date

Every 2.0s: date Sat Dec 24 14:00:05 2016

Sat Dec 24 14:00:05 IST 2016
[root@ArkITShell ~]#watch -d date

Every 2.0s: date Sat Dec 24 14:00:28 2016

Sat Dec 24 14:00:28 IST 2016

watch -d date command output

Using -n option we can provide interval time in seconds, As shown in below df -h command output updates at every 10 seconds. Continuously watch file system increment / decrements. 

[root@ArkITShell ~]# watch -n 10 df -h

Every 10.0s: df -h Sat Dec 24 14:05:30 2016

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 38G 3.5G 35G 10% /
devtmpfs 904M 0 904M 0% /dev
tmpfs 914M 80K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 124M 373M 25% /boot

no-title watch command output. By using -t option along with watch command tittle will be eliminated. 

[root@ArkITShell ~]#watch -t date

Sat Dec 24 14:09:49 IST 2016

To see the effects of quoting, try these out

[root@ArkITShell ~]#watch echo "'"'$$'"'"
[root@ArkITShell ~]#watch echo $$
[root@ArkITShell ~]#watch -n 0.5 echo $$
watch runs commands repeadatlly

watch command output

Real time process updates

[root@ArkITShell ~]# watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15'
real time process updates

real time process updates

Watch command monitor packet transfer using below combination 

[root@ArkITShell ~]# watch -d -n 1 ifconfig

Every 1.0s: ifconfig Sat Dec 24 14:18:01 2016

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet netmask broadcast
 inet6 fe80::20c:29ff:fee0:ae5e prefixlen 64 scopeid 0x20<link>
 ether 00:0c:29:e0:ae:5e txqueuelen 1000 (Ethernet)
 RX packets 8143 bytes 643514 (628.4 KiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 2871 bytes 423670 (413.7 KiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Read More  Linux Boot Process The Millionaire Guide to understand deeply

Monitor Log file continuously for updates 

[root@ArkITShell ~]# watch -n 1 tail /var/log/messages

Know Updated data highlighted in color

[root@ArkITShell ~]# watch -d=cumulative date

See multiple commands output with single watch command

[root@ArkITShell ~]# watch "du -h filename.txt && df -h"
Every 2.0s: du -h file1.txt && df -h Sat Dec 24 14:22:41 2016

296K file1.txt
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 38G 3.5G 35G 10% /
devtmpfs 904M 0 904M 0% /dev
tmpfs 914M 80K 914M 1% /dev/shm
tmpfs 914M 17M 897M 2% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 124M 373M 25% /boot

If data updated then exit from watch command 

[root@ArkITShell ~]# watch -e "! date |grep -m 1 \"Dec\""

Every 2.0s: ! date |grep -m 1 "Dec" Sat Dec 24 14:23:43 2016

Sat Dec 24 14:23:43 IST 2016

command exit with a non-zero status, press a key to exit

Continuously replace upcoming data with required string, In this example i would like to replace ‘A’ character with ‘Ravi’

[root@ArkITShell ~]# watch -n 1 sed 's/A/Ravi/g' file1.txt

That’s about watch runs commands repeatedly.

Related Articles

ps command understanding PID generation

top command to analyze system performance

Thanks for your wonderful Support.

Stay Connected with us. Learn More and Earn More

Good Luck..!!


My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

You may also like...

Leave a Reply