Continuously re-execute a command when it finishes in Bash
I'm trying to figure out a simple way to keep re-executing a command whenever it finishes, for any reason (error or otherwise).
What would be the best way to approach this?
EDIT: I wasn't clear enough before. I need the next execution to wait until the previous one finishes.
EDIT 2: Almost all answers work great. I mistakenly thought most answers would either fork the process or re-execute it just once, neither of which is wanted.
Solution 1:
This creates an infinite loop, executing command
over and over again.
while :
do
command
done
Solution 2:
The watch
command will repeat a command forever with an interval specified:
watch -n0 <command>
Setting -n
to zero effectively puts the interval at nothing (I think it is really .1 seconds).
watch
also has the added benefits of aligning the output so visual changes can be seen easily, and has a switch to highlight changes from the last run.
Reference: the watch man page:
watch runs command repeatedly, displaying its output (the first screenfull). This allows you to watch the program output change over time. By default, the program is run every 2 seconds; use -n or --interval to specify a different interval.
watch will run until interrupted.