Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # max attempts before failure
- ATTEMPTS=5
- if [ $# -gt 1 ]; then
- ATTEMPTS=$1
- shift
- fi
- TIMEOUT=1
- if [ $# -gt 1 ]; then
- TIMEOUT=$1
- shift
- fi
- REINIT_AFTER=3600
- if [ $# -gt 1 ]; then
- REINIT_AFTER=$4
- shift
- fi
- # process to watch
- PROCESS=$1
- STATE_FILE="/var/run/backoff/$PROCESS.state"
- if ! [ -f "$STATE_FILE" ]; then
- mkdir -p $(dirname "$STATE_FILE")
- echo 0 > "$STATE_FILE"
- fi
- function get_time_delta_of_last_attempt {
- let diff=$(date +%s)-$(stat -c %Y "$STATE_FILE")
- echo $diff
- }
- if [ $(get_time_delta_of_last_attempt) -gt $REINIT_AFTER ]; then
- echo 0 > "$STATE_FILE"
- fi
- function with_backoff {
- local max_attempts=$ATTEMPTS
- local timeout=$TIMEOUT
- local attempt=$(cat "$STATE_FILE")
- local exitCode=0
- i=0
- while [ $i -lt $attempt ]; do
- let i=$i+1
- let timeout=$timeout*2
- if [ $timeout -gt $REINIT_AFTER ]; then
- timeout=$REINIT_AFTER
- fi
- done
- let attempt=$attempt+1
- echo $attempt > "$STATE_FILE"
- sleep $timeout
- if [ $attempt -le $max_attempts ] || [ $max_attempts -eq 0 ]; then
- exec "$@"
- fi
- }
- with_backoff "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement