How to repeatedly run bash script every N seconds?

A slight improvement to my comment: if your script exits with true (e.g. when it ends with exit 0), you can run

while script; do sleep 10; done

This is the canonical way to repeat a command as long as it doesn't fail.


In linux you can use the watch program to repeat an action. Assuming that script.sh is executable:

watch -n 10 path/to/script.sh

Would run it every 10 seconds.

To make your script executable, you can use chmod +x script.sh. Don't forget to add the shebang

#!/bin/bash

to the first line (assuming that it's a bash script).

If you're running the script from your current directory, you can then do:

watch -n 10 ./script.sh

Tags:

Linux

Bash