Advance

How to run or repeat a Linux command every X seconds forever

How to run or repeat a Linux command every X seconds forever

A Linux system administrator needs to know some Linux tricks. In this article, you will learn How to run or repeat a Linux command every X seconds forever.

You may need to run a command repeatedly in a certain period of time. Using simple cron commands, such tasks can be easily completed.

Follow this guide to learn simple scripting techniques to monitor or keep an eye on a particular command in continuously running state similar to top command for every 3 seconds by default.

 

How to run or repeat a Linux command every X seconds forever

Let’s do not give up and start discussing the reasons of why you would need to run commands this often and reach to the answer of How to run or repeat a Linux command every X seconds forever.

Recommended Article: Tutorial set up Ansible inventories

Use watch Command

In order to use Watch command, you will be able to execute a command or program periodically and also shows your output on the screen. So you can see the program output in time. The interval can be easily changed to meet your requirements, and also watch re-runs the command/program every 2 seconds. The interval can be easily changed.

 

Monitor Memory Usage

Since using “Watch” is extremely easy, you can test it by firing up a Linux terminal right away and type the following command:

watch free -m

After using the above command, your system free memory will be checked and the result of the free command will be updated every two seconds.

In case you need to hide the header, displaying information about update interval, command that is being executed, and the current time, use the -t option.

Then, use the -n option to change the execution interval to let it specify the interval with which the command will be executed. Soto run your script.sh file every 10 seconds, you can do it like this:

watch -n 10 script.sh

Therefore, after running the above command, you will need to cd to the directory where the script is located or otherwise specify the full path to that script.

Have a look at the following options to see other watch command useful options.

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

 

buy VPS with bitcoin

 

Monitor Logged-In Users, Uptime and Load Average

To continue this step, use the following command.

watch uptime

Press CTRL+C to exit the command.

Here, the ‘uptime‘ command will run and display the updated results every 2 seconds by default.

 

Monitor Progress of Copy Command

As you guess, in Linux while copying files from one location to others using cp command, the progress of data is not shown. Use the watch command, to see the progress of data being copied.

cp ubuntu-15.10-desktop-amd64.iso /home/noodi/ &  watch -n 0.1 du -s /home/tecmint/ubuntu-15.10-desktop-amd64.iso
Alternatively, you can go to the Advance copy command, which shows the progress of data while copying.

 

Use sleep Command

Sleep is used in many useful situations, Two of the are often for debugging shell scripts and when combined with for or while loops, you can get pretty awesome results.

But do not worry if this is the first time you hear about the “sleep” command, it is used to delay something for a specified amount of time. In scripts, you can use it to tell your script to run command 1, wait for 10 seconds, and then run command 2.

Having the above loops, you can tell bash to run a command, sleep for N amount of seconds and then run the command again.

Let’s see examples of both loop:

for loop Example

for i in {1..10}; do echo -n "This is a test in loop $i "; date ; sleep 5; done

The above one-liner, will run the echo command and display the current date, a total of 10 times, with 5 seconds sleep between executions.

Output

This is a test in loop 1 Wed Feb 17 20:49:47 EET 2016  This is a test in loop 2 Wed Feb 17 20:49:52 EET 2016  This is a test in loop 3 Wed Feb 17 20:49:57 EET 2016  This is a test in loop 4 Wed Feb 17 20:50:02 EET 2016  This is a test in loop 5 Wed Feb 17 20:50:07 EET 2016  This is a test in loop 6 Wed Feb 17 20:50:12 EET 2016  This is a test in loop 7 Wed Feb 17 20:50:17 EET 2016  This is a test in loop 8 Wed Feb 17 20:50:22 EET 2016  This is a test in loop 9 Wed Feb 17 20:50:27 EET 2016  This is a test in loop 10 Wed Feb 17 20:50:32 EET 2016

Also, please consider that you are able to change the echo and date commands with your own commands or script and change the sleep interval per your needs.

 

while loop Example

while true; do echo -n "This is a test of while loop";date ; sleep 5; done

Output

This is a test of while loopWed Feb 17 20:52:32 EET 2016  This is a test of while loopWed Feb 17 20:52:37 EET 2016  This is a test of while loopWed Feb 17 20:52:42 EET 2016  This is a test of while loopWed Feb 17 20:52:47 EET 2016  This is a test of while loopWed Feb 17 20:52:52 EET 2016  This is a test of while loopWed Feb 17 20:52:57 EET 2016

Until it is either killed or interrupted by the user, the above command will run. It is so useful in case you want to run a command running in the background and you don’t want to count on corn.

 

Please Note: When using the above methods, it is highly recommended that you set an interval long enough to give enough time of your command to finish running, before the next execution.

Please Consider: The samples in this tutorial are useful but are not meant to completely replace the cron utility. It is up to you to find which one works better for you, but if we have to separate the usage of both techniques, I would say this:

  • Use cron when you need to run commands periodically even after system reboots.
  • Use the methods explained in this tutorial for programs/scripts that are meant to run within the current user session.

 

Dear user, we wish this tutorial How to run or repeat a Linux command every X seconds forever would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.

View More Posts
Tom Veitch
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day