How to run Linux commands every X seconds

How to run Linux commands every X seconds

In this article we will explain how to run Linux Linux commands every X seconds and how System administrators runs commands for certain period of time.

Options of watch command

-b &ndash creates a beep sound if the exit of the command is non-zero.
-c &ndash Interprets ANSI color sequences.
-d &ndash highlights the changes in the command output.

Using Watch Command

It helps to execute the commands every 2 seconds.

To Monitor Memory usage

watch free -m

Output

Every 2.0s: free -m                                     Sat Mar  5 00:01:59 2016

                     total    used       free     shared     buffers     cached
Mem:                 988        533        455      3          32          201
-/+ buffers/cache:   299        688
Swap:                1999       0          1999

-n option is used to specify the interval when the command will be executed. -d option is used to highlight the changes in the output.

Every 5.0s: sh script.sh                                Sat Mar  5 00:13:55 2016

total 596
-rw-rw-r-- 1 user1 user1     14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1  297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1  304451 Mar  5 00:13 watch 2.png
                        total         used        free     shared    buffers     cached
Mem:                    988          545         443      4         33          208
-/+ buffers/cache:      303          684
Swap:                   1999         0           1999

To monitor logged-in users

Example

[user1@linuxhelp ~]$ watch uptime

Every 2.0s: uptime                                      Sat Mar  5 00:17:32 2016

00:17:32 up  1:18,  2 users,  load average: 0.00, 0.00, 0.00

To exit the command

Press CTRL+C

The up time command will run and display the updated commands results every 2 seconds.

To monitor progress copy command

Example

[user1@linuxhelp Desktop]$ cp ubuntu-10.04.1-server-amd64.iso /home/user1/new/ 
[user1@linuxhelp Desktop]$ watch -n 2 du -s /home/user1/new/
Every 2.0s: du -s /home/user1/new/                  Sat Mar  5 00:31:10 2016

698880  /home/user1/new/

Using sleep Command for loop

Example

[user1@linuxhelp ~]$ for i in {1..3}  do ll   date   sleep 3   done
total 700076
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
Sat Mar  5 00:48:35 IST 2016
total 700076
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
Sat Mar  5 00:48:39 IST 2016
total 700076
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
Sat Mar  5 00:48:42 IST 2016

Using while loop

Example

[user1@linuxhelp ~]$ while true  do ll   date   sleep 3  done 
total 700444
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
-rw-rw-r-- 1 user1 user1    374098 Mar  5 00:51 watch 5.png
Sat Mar  5 00:57:23 IST 2016
total 700444
drwxrwxr-x 2 user1 user1      4096 Mar  5 00:28 new
-rw-rw-r-- 1 user1 user1        14 Mar  5 00:05 script.sh
-rw-rw-r-- 1 user1 user1 715644928 Sep 30  2010 ubuntu-10.04.1-server-amd64.iso
-rw-rw-r-- 1 user1 user1    297690 Mar  5 00:01 watch 1.png
-rw-rw-r-- 1 user1 user1    304451 Mar  5 00:13 watch 2.png
-rw-rw-r-- 1 user1 user1    287944 Mar  5 00:17 watch 3.png
-rw-rw-r-- 1 user1 user1    322083 Mar  5 00:31 watch 4.png
-rw-rw-r-- 1 user1 user1    374098 Mar  5 00:51 watch 5.png
Sat Mar  5 00:57:26 IST 2016
total 70044
FAQ
Q
How to Monitor Memory usage from command line?
A
To Monitor Memory usage from command line use the following command

#free -m
Q
I want this command
# cat /proc/mdstat
to run every 2 sec automatically and repetitively how can it be done?
A
You can make use of the following command
watch -n 2 cat /proc/mdstat
Q
How to Run a command after a delay of n mins?
A
use the following command
'sleep n && command' or 'at'
Q
How to Repeat a Unix command every x seconds forever?
A
watch is the best command to Repeat a Unix command every x seconds forever
Q
Can you tell me "Is there any command in solaris that gives the output repeatedly for every x seconds" when used with other commands like ls,du,df,etc..Like prstat updates its output for ever
A
Use the following command
yes "; sleep 5" | sh