0

I want to retry a command for 5 times with an interval of 20 seconds. I want this command to be passed as a method parameter. How to do it? And once the function is written how to pass the value to the function?

I want my current code to be converted to a function which takes a set of parameters. How to write and call this function in my shell script?

My current code is like this :

  trialNumber=0
    until [ $trialNumber -ge 5]  
     do
        ssh $USERID@$HOST $SCRIPT_LOCATION/runme.sh   # This line is my command and it may very with number of parameters or command itself.
       [ $? -eq 0 ] && break
      trialNumber=$[$trialNumber+1]
      sleep 20
   done

( Above code is embedded at many places I want to move it into a function).

5
  • As a side note. The code need to be improved. Though you could wrap it in a function like TrytoSSH() { your_code_here } then call your function by TrytoSSH Commented Aug 19, 2013 at 11:13
  • Thanks val0x00ff, what about passing parameters and using them inside the method? For example can I call trytoSSH $USERID@$HOST $SCRIPT_PATH/runme.sh and use them in my function with $1, $2 , $3 ? And sometimes parameters to ssh might be different in number also so I want "ssh $USERID@$HOST $SCRIPT_LOCATION/runme.sh " or ssh -t $USERID@$HOST $SCRIPT_LOCATION/runme.sh " as single parameter and want to replace it directly with ssh.
    – java_enthu
    Commented Aug 19, 2013 at 11:27
  • You certainly can. You can use "$1" "$2" positional parameters to achieve this. The command will be like ssh -t "$1" "$2" where positional parameter "$1" is user@host and "$2" is your script. Commented Aug 19, 2013 at 11:30
  • @val0x00ff Is quoting the positional parameter really necessary? With spaces you are ****ed already before passing the parameters. Other side note to java_enthu: why not using a for loop?
    – Bernhard
    Commented Aug 19, 2013 at 11:41
  • @Bernhard Not really, it's more a habit. I always quote the vars even though not needed. Commented Aug 19, 2013 at 11:49

2 Answers 2

3
retry() {
  trialNumber=$1 delay=$2; shift 2
  while [ "$trialNumber" -gt 0 ]; do
    "$@" && return
    ret=$?
    sleep "$delay"
    trialNumber=$(($trialNumber - 1))
  done
  return "$ret"
}

retry 5 20 ssh "$USERID@$HOST" "$SCRIPT_LOCATION/runme.sh"

Though the last sleep in case of failure is not necessary. May be better as:

retry() {
  trialNumber=$1 delay=$2; shift 2
  until "$@"; do
    ret=$?
    trialNumber=$(($trialNumber - 1))
    [ "$trialNumber" -gt 0 ] || return "$ret"
    sleep "$delay"
  done
}
3
  • Hi Stephane,looks good to me. Thanks a bunch. I put this code in a shell script and in the shell script I am trying to call this function but I get line:6 command:retry not found. (function definition of retry starts at line 6 in my script).
    – java_enthu
    Commented Aug 19, 2013 at 11:44
  • Thanks it worked. I did not know that method should be declared only at the top. :-). Merci mille fois.
    – java_enthu
    Commented Aug 19, 2013 at 11:57
  • Stefan : I've another scenario : SCRIPT_FILES =ssh $USER@HOST find $SCRIPTS -maxdepth 2 -type f for SCRIPT in $SCRIPT_FILES ..... Can I use the same retry function here also , I mean : SCRIPT_FILES =5 20 ssh $USER@HOST find $SCRIPTS -maxdepth 2 -type f ???
    – java_enthu
    Commented Aug 19, 2013 at 16:33
1

Use the retry command.

~$ retry --times 5 --delay 20 -- ssh $USERID@$HOST $SCRIPT_LOCATION/runme.sh

https://github.com/minfrin/retry

Available out the box in recent Debian, Ubuntu, and Nix.

You must log in to answer this question.

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