How to make the cyclic execution of the script? At the end of the script i need to run it again. Cron is not suitable, because i need to run the script immediately after the previous

  • Do you need to run it indefinite amount or times or you know how many times to do it ? Do you need to run it only of the previous fails or exit status doesn't matter ? May 3, 2016 at 13:37
  • @Serg, Yes, i need to run it an infinite number of times. I need to run it if previous fails or been executed
    – Artem Y
    May 3, 2016 at 13:40
  • ok. I'll throw some ideas in a minute May 3, 2016 at 13:43
  • 1
    @Rikaz inside the script, you can easily make the script run in an infinite while loop, which is a simpeler solution than actually placing the loop outside the script. May 3, 2016 at 14:17
  • 2
    True , here you just can do the while loop I've proposed and run python script.py instead of /path/to/script.sh as in my answer. You could put the script itself into while loop, or if it throws exceptions - combine while with try . . .except.. . . structure. May 3, 2016 at 14:19

3 Answers 3


The basic control structure in any sort of programming language that requires indefinite looping is while loop.

while true ; do /path/to/script.sh ; if [ $? -ne 0    ] ; then continue ; else break ; fi ; done

More readable formating would be :

while true 
   /path/to/script.sh  # Ensure your script actually outputs exit status
   if [ $? -ne 0    ] ; then  
      continue  # if exit status not 0 ( not success ) , repeat
       break    # if successful - exit

Of course you need to ensure that your script actually does have return status that equals to 0 on success. You may or may not want to use full path, or ./ operator to run script in current directory , or specify interpreter such as python /my/python/script.py

If necessary you can add delay before next iteration starts. To do that, you can place sleep 0.250 right after fi but before done


Run them as:

/path/to/script.sh && /path/to/script.sh

The second one will run if the first one succeeds.

Without depending on the success of the first one:

/path/to/script.sh; /path/to/script.sh

Run second one only if the first one fails:

/path/to/script.sh || /path/to/script.sh

For real cyclic execution, you can use a recursive function:

run_script () { /path/to/script.sh || run_script ;}
  • Good use of recursive call in the function, +1 for that , but make sure you test the exit status so that to have a stopping point for the recursion. See the comments under the question May 3, 2016 at 13:56
  • @Serg edited....
    – heemayl
    May 3, 2016 at 13:57
  • 1
    Wouldn't unlimited recursion cause bash to crash eventually?
    – n0rd
    May 3, 2016 at 18:40
  • @n0rd It will wait for the script to complete and based on the exit status (if false), then the function meaning the script will run..so a single process will be running at a given time until the recursion depth is reached.., I don't think that's a major problem...
    – heemayl
    May 3, 2016 at 18:43
  • 1
    How exactly crashing the interpreter (the bash process running the script will terminate) is not a major problem? Yes, if script.sh takes significant time to execute, time to crash will be quite long, but it will happen. If bash did the tail recursion optimization, that would be indeed a nice use of recursion, but it does not.
    – n0rd
    May 3, 2016 at 18:50

To make your script execute again and again you can do it like this (make sure th script has chmod 755 set):

echo "test" #or whatever you do in your script
exec sh <scriptname>

You can stop your script ctrl+c.

Note that the use of exec means that there will be at most one process running at a time, instead of having a new process added each time the script runs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .