Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
1  
You still aren't clear. It'd help if you mentioned what program you're executing. –  Daniel Beck Nov 27 '11 at 13:38
    
-1 This question does not show any research effort, as the user commented that answers didn't work instead of actually trying to do what they suggest. –  Daniel Beck Nov 30 '11 at 20:04

4 Answers 4

up vote 14 down vote accepted

This creates an infinite loop, executing command over and over again.

while :
    do
        command
done
share|improve this answer
    
Please see the edit to the original question. I need to keep re-executing it over and over again, not just once. –  user107012 Nov 27 '11 at 13:31
7  
@calcifer This answer works quite well. There's something unusual about your program that none of the four users answering so far anticipated and you failed to mention it. –  Daniel Beck Nov 27 '11 at 13:39
    
@calcifer this is an infinite loop. it keeps repeating until the end of time (well, until the script is terminated anyways) –  Nate Koppenhaver Nov 27 '11 at 19:44
1  
My bad, I thought this (and all the other answers) will either fork the process or re-execute it just once, neither of which is wanted. So yes, this answer works. Thanks. –  user107012 Nov 27 '11 at 19:48

You can plan ahead during command execution (provided it isn't interactive) and enter !! which will execute the previous command again. This works e.g. during ping -c 5.


You can also define a function in bash:

function repeat { "$@"; "$@"; }

To persist it, store it in ~/.bashrc.

Then, you can run your command like this:

repeat ping -c5 heise.de

If it's a specific command you want to repeatedly execute (and not e.g. any command), you can replace "$@" in that snippet with your actual command, and name the function e.g. repeat_ping instead.


To make it an infinite loop, you can do what @Dennis suggests. I recommend you add a waiting period if you intend to use this in an interactive shell, like this:

function repeat { while 1 ; do "$@" ; sleep 1 ; done; }

Otherwise it's rather inconvenient to abort this infinite loop using Ctrl-Z.

share|improve this answer
    
Please see the edit to the original question. –  user107012 Nov 27 '11 at 13:30
5  
@calcifer These executions wait until the program call returns. Does your program detach from the shell and return before it quits? –  Daniel Beck Nov 27 '11 at 13:31
    
My bad, I thought this (and all the other answers) will either fork the process or re-execute it just once, neither of which is wanted. So yes, this answer works. Thanks. –  user107012 Nov 27 '11 at 19:48

A simple solution would be:

yourcommand; !#

; separates commands, allowing for multiple commands in one line (Bash: Lists)

!# tells bash to "repeat everything I have written so far in this line" (Bash: Event-Designators)

share|improve this answer
    
Sorry, I wasn't clear enough. I need to keep re-executing it over and over again, not just once. –  user107012 Nov 27 '11 at 13:27
    
but this is still cool bash feature! –  scythargon Jun 12 at 12:34

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.

share|improve this answer
    
Please see the edit to the original question. –  user107012 Nov 27 '11 at 13:30
6  
All of the answers so far do this, including this one. Linux won't fork unless you specifically ask it to. –  Paul Nov 27 '11 at 13:34
    
I didn't know that about fork, thanks. –  user107012 Nov 27 '11 at 19:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.