GNU/Linux bash: execute a command every X seconds indefinitely (without cron)
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.



Comment's better than post !
Comment's better than post !
Thanks for your contribution.
Thanks for your contribution. I didn't know about the watch command. Nice one. Thanks again!
This command will do the
This command will do the same:
watch -n 5 echo "Hello World"
Post new comment