Skip to content

Instantly share code, notes, and snippets.

@jim3ma
Forked from felipou/retry.sh
Created November 15, 2021 06:52
Show Gist options
  • Select an option

  • Save jim3ma/3150753914d020e08216df574ac0c82b to your computer and use it in GitHub Desktop.

Select an option

Save jim3ma/3150753914d020e08216df574ac0c82b to your computer and use it in GitHub Desktop.

Revisions

  1. @felipou felipou revised this gist Mar 15, 2021. 1 changed file with 15 additions and 3 deletions.
    18 changes: 15 additions & 3 deletions retry.sh
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ function retry() {
    MAX_RETRIES="10"

    # Command-line arguments parsing
    while [[ $# > 1 ]]
    while [[ $# > 0 ]]
    do
    key="$1"

    @@ -33,6 +33,12 @@ function retry() {
    MAX_RETRIES="$2"
    shift # past argument
    ;;
    -h|--help)
    echo """A retry command for bash.
    Usage:
    retry [-s SLEEP_TIME] [-m MAX_RETRIES] COMMAND_WITH_ARGUMENTS"""
    return 0
    ;;
    *)
    break # unknown option
    ;;
    @@ -41,7 +47,13 @@ function retry() {
    done

    # The command is all remaining arguments
    COMMAND=`printf "%q\n" "$@"`
    COMMAND=`printf "%q " "$@"`

    if [ "$(echo $COMMAND)" == "''" ]
    then
    echo "No command given"
    return 2
    fi

    echo $COMMAND

    @@ -61,7 +73,7 @@ function retry() {
    if [ $i -eq $MAX_RETRIES ]
    then
    echo "Max retries reached"
    return 1
    return 1
    fi
    }

  2. @felipou felipou revised this gist Mar 15, 2021. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion retry.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #!/bin/bash
    #
    # Created by Felipe Machado - 2016/02/14
    #
    @@ -40,7 +41,7 @@ function retry() {
    done

    # The command is all remaining arguments
    COMMAND="$@"
    COMMAND=`printf "%q\n" "$@"`

    echo $COMMAND

    @@ -60,5 +61,8 @@ function retry() {
    if [ $i -eq $MAX_RETRIES ]
    then
    echo "Max retries reached"
    return 1
    fi
    }

    retry "$@"
  3. @felipou felipou created this gist Feb 14, 2016.
    64 changes: 64 additions & 0 deletions retry.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #
    # Created by Felipe Machado - 2016/02/14
    #
    # A retry command for bash
    # Retries the given command up to MAX_RETRIES, with an interval of SLEEP_TIME
    # between each retry. Just put it on your bash_profile and be happy :)
    # Usage:
    # retry [-s SLEEP_TIME] [-m MAX_RETRIES] COMMAND_WITH_ARGUMENTS
    #
    # Codes used as reference:
    # - http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
    # - https://gist.github.com/iangreenleaf/279849
    #


    # Retry command
    function retry() {
    SLEEP_TIME="30"
    MAX_RETRIES="10"

    # Command-line arguments parsing
    while [[ $# > 1 ]]
    do
    key="$1"

    case $key in
    -s|--sleep)
    SLEEP_TIME="$2"
    shift # past argument
    ;;
    -m|--max)
    MAX_RETRIES="$2"
    shift # past argument
    ;;
    *)
    break # unknown option
    ;;
    esac
    shift # past argument or value
    done

    # The command is all remaining arguments
    COMMAND="$@"

    echo $COMMAND

    i=0

    # I'm using eval to allow for pipes. This could become an option
    eval $COMMAND

    while [ $? -ne 0 -a $i -lt $MAX_RETRIES ]
    do
    echo "Command failed - retrying in $SLEEP_TIME..."
    sleep $SLEEP_TIME
    i=$(($i+1))
    eval $COMMAND
    done

    if [ $i -eq $MAX_RETRIES ]
    then
    echo "Max retries reached"
    fi
    }