GNU/Linux bash: execute a command every X seconds indefinitely (without cron)

Submitted by fabio on Sat, 2010-01-16 11:38.

If you use the GNU/Linux operating system intensely as development environment or as a server you'll probably find yourself in need of running a determinate command or script every X seconds forever.

This could be useful for example to check that a service you run on your server is working correctly (e.g. your Apache httpd server), to clear some cache your system is using or to periodically check for software updates. Personally I needed this to periodically update the DNS entry of my home server with its current IP (my ISP gives dynamic IP, not static ones).

Probably the best approach to implement this would be using Cron. But, in some cases, you just need a quick and easier way to do that.

Well, it's actually a matter of a simple 5 lines Bash shell script:

while [ 1 ];
  echo "Hello World";
  sleep 5;

The above script will run the command echo "Hello World" indefinitely every 5 seconds. Just substitute the echo command with the one you need and adjust the sleep time with the amount of seconds you need.

Posted in:

my watch is broken

Submitted by Anonymous (not verified) on Wed, 2013-09-04 00:40.


This is my command

watch -n 1 ~/Scripts/

and this is my script,

find . -name "*.o" | xargs ls -l
echo $(date)

and it works once, then I get a

xargs: ls: terminated by signal 13
xargs: ls: terminated by signal 13



Comment's better than post !

Submitted by Anonymous (not verified) on Thu, 2013-01-10 13:04.

Comment's better than post !

Thanks for your contribution.

Submitted by fabio on Fri, 2010-01-22 11:58.

Thanks for your contribution. I didn't know about the watch command. Nice one. Thanks again!

This command will do the

Submitted by Anonymous (not verified) on Fri, 2010-01-22 10:18.

This command will do the same:

watch -n 5 echo "Hello World"