Thursday, April 14, 2016

[Quick Tips: Repeat command Automatically] : Run a Linux command every X seconds forever using Watch command

Run a Linux command every X seconds forever using Watch command

Have you ever wanted to run a command every few seconds automatically? Of course you can do this using a shell script or cron jobs. Alternatively, you can repeat a Linux command at a particular interval without having to manually run it. Here comes watch command in handy.
Watch command can be used to execute a program every X seconds forever and it displays the outputs in the console. By default, the program is run every 2 seconds, Or you can define the time interval of your choice. It will keep running and displaying the respective results until you terminate the program by pressing CTRL+C or kill the process or force your system to reboot.

Syntax:

watch [options] command
Here is the set of five examples that explains where can you use watch command.

Example 1:

Let us display the output of the ‘uptime’ command:
watch uptime
Sample output:
Every 2.0s: uptime                                      Wed Feb  3 12:56:46 2016

 12:56:46 up  2:14,  2 users,  load average: 0.80, 0.87, 0.63
Here,
  • Every 2.0s: uptime – The ‘uptime’ command will run every 2 seconds and display the result.
  • Wed Feb  3 12:56:46 2016 – The current date and time when we executed the command.
To exit the command, press CTRL+C.
To save the output of the uptime in a file, run:
watch `uptime` > uptime.txt
Or
watch `uptime` > uptime.doc

Example 2:

Let us display the file system disk space usage.
As I mentioned before, watch command executes a program every 2 seconds by default. We can change it to a particular interval, for example 5 seconds, using ‘-n’ parameter.
watch -n 5 df -h
Sample output:
Every 5.0s: df -h                                        Wed Feb  3 12:57:23 2016

Filesystem                    Size  Used Avail Use% Mounted on

udev                          1.9G  4.0K  1.9G   1% /dev

tmpfs                         387M  1.3M  385M   1% /run

/dev/mapper/lubuntu--vg-root  455G  350G   82G  81% /

none                          4.0K     0  4.0K   0% /sys/fs/cgroup

none                          5.0M     0  5.0M   0% /run/lock

none                          1.9G   44M  1.9G   3% /run/shm

none                          100M   28K  100M   1% /run/user

/dev/sda1                     236M   51M  173M  23% /boot
To check this command, just create or delete any file or folder, you might notice that the free space has changed in the output after creating or deleting the files/folders.

Example 3:

To watch contents of a directory change, run:
watch -d ls -l
Here, The -d or –differences flag will highlight the differences between successive updates.
Sample output:
Every 2.0s: ls -l                                       Wed Feb  3 12:36:52 2016

total 96

drwxr-xr-x  2 sk sk  4096 Dec 14 20:12 Desktop

drwxr-xr-x  2 sk sk  4096 Oct  3 19:08 Documents

drwxr-xr-x 13 sk sk 12288 Feb  3 11:12 Downloads

drwxrwxr-x  4 sk sk  4096 Jun  5  2015 Entertainment

drwxr-xr-x  2 sk sk  4096 Dec 27 17:44 Music

drwxrwxr-x 11 sk sk  4096 Dec 11 15:33 Official

drwxrwxr-x 16 sk sk  4096 Nov  6 18:06 Personal

drwxr-xr-x  3 sk sk  4096 Feb  2 18:58 Pictures

drwxr-xr-x  2 sk sk 12288 Dec 15 11:23 Public

drwxrwxr-x  6 sk sk  4096 Jan  8 14:21 Soft_Backup

drwxr-xr-x  2 sk sk  4096 Nov 10  2014 Templates

drwxr-xr-x  2 sk sk  4096 Feb 25  2015 Videos

drwxrwxr-x  9 sk sk  4096 Feb  2 18:58 VirtualBox VMs
Also, you can display the contents of a directory change which is owned by a particular user (Ex.ostechnix).
watch -d 'ls -l | fgrep ostechnix'

Example 4:

To display the memory details, run:
watch -d free -om
Sample output:
Every 2.0s: free -om                                    Wed Feb  3 12:39:57 2016
             total       used       free     shared    buffers     cached

Mem:          3860       3765         95        152        202       1250

Swap:         3999          0       3999

Example 5:

To display the output of du command every 10 seconds, you could use:
watch -n 10 du -h
Sample output:
Every 10.0s: du -h                                                                                                                              Wed Feb  3 12:44:14 2016
17M     ./.disruptive innovations sarl/bluegriffon/q87d9o6v.default/extensions

28M     ./.disruptive innovations sarl/bluegriffon/q87d9o6v.default

28M     ./.disruptive innovations sarl/bluegriffon

28M     ./.disruptive innovations sarl

4.0K    ./Music

28K     ./.gnome/apps

32K     ./.gnome

12K     ./Public

36K     ./.filezilla

80K     ./.java/fonts/1.7.0_79

84K     ./.java/fonts

8.0K    ./.java/.userPrefs/sftp-mss

12K     ./.java/.userPrefs

100K    ./.java

4.0K    ./.dooble/Cache

16K     ./.dooble/Dooble

4.0K    ./.dooble/Histories

176K    ./.dooble

4.0K    ./.PlayOnLinux/wine/mono

13M     ./.PlayOnLinux/wine/gecko

4.0K    ./.PlayOnLinux/wine/linux-amd64

652K    ./.PlayOnLinux/wine/linux-x86/1.3.19/share/wine/fonts

872K    ./.PlayOnLinux/wine/linux-x86/1.3.19/share/wine

Conclusion

Watch command will be very handy when it comes to monitor disk usage and memory usage. Do not confuse this command with other monitoring tools. This command is intend to execute a command every particular second forever, until you manually stops it.
For more details, I recommend you to refer man pages.
man watch

 

No comments: