[linux] Repeat command automatically in Linux

Is it possible in Linux command line to have a command repeat every n seconds?

Say, I have an import running, and I am doing

ls -l

to check if the file size is increasing. I would like to have a command to have this repeat automatically.

This question is related to linux bash ubuntu

The answer is


If you want to avoid "drifting", meaning you want the command to execute every N seconds regardless of how long the command takes (assuming it takes less than N seconds), here's some bash that will repeat a command every 5 seconds with one-second accuracy (and will print out a warning if it can't keep up):

PERIOD=5

while [ 1 ]
do
    let lastup=`date +%s`
    # do command
    let diff=`date +%s`-$lastup
    if [ "$diff" -lt "$PERIOD" ]
    then
        sleep $(($PERIOD-$diff))
    elif [ "$diff" -gt "$PERIOD" ]
    then
        echo "Command took longer than iteration period of $PERIOD seconds!"
    fi
done

It may still drift a little since the sleep is only accurate to one second. You could improve this accuracy by creative use of the date command.


watch is good but will clean the screen.

watch -n 1 'ps aux | grep php'

while true; do
    sleep 5
    ls -l
done

You can run the following and filter the size only. If your file was called somefilename you can do the following

while :; do ls -lh | awk '/some*/{print $5}'; sleep 5; done

One of the many ideas.


sleep already returns 0. As such, I'm using:

while sleep 3 ; do ls -l ; done

This is a tiny bit shorter than mikhail's solution. A minor drawback is that it sleeps before running the target command for the first time.


If the command contains some special characters such as pipes and quotes, the command needs to be padded with quotes. For example, to repeat ls -l | grep "txt", the watch command should be:

watch -n 5 'ls -l | grep "txt"'


A concise solution, which is particularly useful if you want to run the command repeatedly until it fails, and lets you see all output.

while ls -l; do
    sleep 5
done

If you want to do something a specific number of times you can always do this:

repeat 300 do my first command here && sleep 1.5

watch -n 5 'ls -l 

Will Runls -l command after every 5s

Output :-

Every 5.0s: ls -l                                                                                                      Fri Nov 17 16:28:25 2017

total 169548
-rw-rw-r--  1 sachin sachin     4292 Oct 18 12:16 About_us_Admission.doc
-rw-rw-r--  1 sachin sachin      865 Oct 13 15:26 About_us_At_glance.doc
-rw-rw-r--  1 sachin sachin     1816 Oct 13 16:11 About_us_Principle.doc
-rw-rw-r--  1 sachin sachin     1775 Oct 13 15:59 About_us_Vission_mission.doc
-rw-rw-r--  1 sachin sachin     1970 Oct 13 16:41 Academic_Middle_school.doc
-rw-rw-r--  1 sachin sachin      772 Oct 16 16:07 academics_High_School.doc
-rw-rw-r--  1 sachin sachin      648 Oct 16 13:34 academics_pre_primary.doc
-rw-rw-r--  1 sachin sachin      708 Oct 16 13:39 academics_primary.doc
-rwxrwxr-x  1 sachin sachin     8816 Nov  1 12:10 a.out
-rw-rw-r--  1 sachin sachin    23956 Oct 23 18:14 Ass1.c++
-rw-rw-r--  1 sachin sachin      342 Oct 23 22:13 Ass2.doc
drwxrwxr-x  2 sachin sachin     4096 Oct 19 10:45 Backtracking
drwxrwxr-x  3 sachin sachin     4096 Sep 23 20:09 BeautifulSoup
drwxrwxr-x  2 sachin sachin     4096 Nov  2 00:18 CL_1
drwxrwxr-x  2 sachin sachin     4096 Oct 23 20:16 Code
drwxr-xr-x  2 sachin sachin     4096 Nov 15 12:05 Desktop
-rw-rw-r--  1 sachin sachin        0 Oct 13 23:12 doc
drwxr-xr-x  4 sachin sachin     4096 Nov  6 21:18 Documents
drwxr-xr-x 27 sachin sachin    12288 Nov 17 13:23 Downloads
-rw-r--r--  1 sachin sachin     8980 Sep 19 23:58 examples.desktop

To minimize drift more easily, use:

while :; do sleep 1m & some-command; wait; done

there will still be a tiny amount of drift due to bash's time to run the loop structure and the sleep command to actually execute.

hint: ':' evals to 0 ie true.


"watch" does not allow fractions of a second in Busybox, while "sleep" does. If that matters to you, try this:

while true; do ls -l; sleep .5; done

Running commands periodically without cron is possible when we go with while.

As a command:

while true ; do command ; sleep 100 ; done &
[ ex: # while true;  do echo `date` ; sleep 2 ; done & ]

Example:

while true
do echo "Hello World"
sleep 100
done &

Do not forget the last & as it will put your loop in the background. But you need to find the process id with command "ps -ef | grep your_script" then you need to kill it. So kindly add the '&' when you running the script.

# ./while_check.sh &

Here is the same loop as a script. Create file "while_check.sh" and put this in it:

#!/bin/bash
while true; do 
    echo "Hello World" # Substitute this line for whatever command you want.
    sleep 100
done

Then run it by typing bash ./while_check.sh &


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found