Skip to content


Bash Launcher Loop

Have you written a bash script that executes a command in a loop, maybe to relaunch a process after it dies? What do you do when that process keeps dying?

Enter the Bash Launcher Loop:

#!/bin/bash

# maximum number of attempts to launch
max_launches=2
# maximum number of launches allowed in $reset seconds
runaway_max=8
# minimum
min_seconds=1
# number of seconds to let process run before reset
reset_seconds=10

# number of attempts to launch
launch_attempts_count=0
start_time=`date +%s`

while true; do

  echo "Executing ($launch_attempts_count)..."
#  sleep 1

  # increment the number of launch attempts
  launch_attempts_count=$(($launch_attempts_count+1))
  # get the number of seconds since the start time...
  time=$((`date +%s`-$start_time))
  # if we launched more than $max_launches in less than $min_seconds, then
  # for some reason the process can't start, so exit
  if [ $launch_attempts_count -gt $max_launches -a $time -lt $min_seconds ]; then
    echo "Too many launches!"
    exit 1
  fi
  # If we haven't exceeded the threshold for failed starts, but
  # the process isn't running long enough to reset the counters,
  # we can guess there's a problem that keeps causing the process
  # to crash, so we should probably give up for now
  if [ $launch_attempts_count -gt $runaway_max ]; then
    echo "Process keeps crashing"
    exit 1
  fi
  # If the process has been running for more than $max_seconds, then
  # we're going to assume it was working properly, so reset the counters.
  # Normally, $max_seconds would be set to something like 3600 (1 hour).
  if [ $time -gt $reset_seconds ]; then
    launch_attempts_count=0
    start_time=`date +%s`
  fi

done

The script above will stop trying to launch the process if:

  • if there are more than 2 launch attempts in 1 second (printing “Too many launches!)
  • if there are more than 8 launch attempts in 10 seconds (printing “Process keeps crashing!)

If the process is relaunched less than 8 times in 10 seconds, the $start_time and $launch_attempts_count will be reset, thus starting over.

To see this in action just run the script like it is, and the output will be:

Executing (0)...
Executing (1)...
Executing (2)...
Too many launches!

Now uncomment line 19 (# sleep 1), and the output will be:

Executing (0)...
Executing (1)...
Executing (2)...
Executing (3)...
Executing (4)...
Executing (5)...
Executing (6)...
Executing (7)...
Executing (8)...
Process keeps crashing

Finally, change line 19 from sleep 1 to sleep 2, and the output will be:

Executing (0)...
Executing (1)...
Executing (2)...
Executing (3)...
Executing (4)...
Executing (5)...
Executing (0)...
Executing (1)...
Executing (2)...
Executing (3)...
Executing (4)...
Executing (5)...
Executing (0)...

And so on, until you hit ctrl+c.

Obviously those values are just for demo purposes. Better production values would be:

max_launches=2
runaway_max=2
min_seconds=1
reset_seconds=3600

This tells the script to exit if there are more than two launch attempts in one second or more than two in one hour, otherwise it will just keep running.

Posted in Bash, Linux, Tips and Tricks.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.