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 ];
do 
  echo "Hello World";
  sleep 5;
done

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.

Hi,

This is my command

watch -n 1 ~/Scripts/obj_watch.sh

and this is my script,

#!/bin/bash
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

periodically.

Bueller.

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"

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a personal or company website insert its address in the form http://www.example.com/ .
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <small> <del> <img> <h2> <h3> <h4> <b> <video> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options