如何在发生错误时自动重新运行“curl”命令

时间:2011-12-02 02:10:17

标签: linux bash curl

有时当我使用curl命令执行bash脚本将某些文件上传到我的ftp服务器时,它会返回一些错误,如:

56 response reading failed

我必须找到错误的行并手动重新运行它们就可以了。

我想知道是否可以在发生错误时自动重新运行。


我的脚本是这样的:

#there are some files(A,B,C,D,E) in my to_upload directory,
# which I'm trying to upload to my ftp server with curl command
for files in `ls` ;
    do curl -T $files ftp.myserver.com --user ID:pw ;
done

但有时A,B,C会成功上传,只有D留下“错误56”,所以我必须手动重新运行curl命令。此外,正如Will Bickford所说,我更喜欢不需要确认,因为我在脚本运行时总是睡着了。 :)

3 个答案:

答案 0 :(得分:39)

这是我用来执行指数退避的bash片段:

# Retries a command a configurable number of times with backoff.
#
# The retry count is given by ATTEMPTS (default 5), the initial backoff
# timeout is given by TIMEOUT in seconds (default 1.)
#
# Successive backoffs double the timeout.
function with_backoff {
  local max_attempts=${ATTEMPTS-5}
  local timeout=${TIMEOUT-1}
  local attempt=1
  local exitCode=0

  while (( $attempt < $max_attempts ))
  do
    if "$@"
    then
      return 0
    else
      exitCode=$?
    fi

    echo "Failure! Retrying in $timeout.." 1>&2
    sleep $timeout
    attempt=$(( attempt + 1 ))
    timeout=$(( timeout * 2 ))
  done

  if [[ $exitCode != 0 ]]
  then
    echo "You've failed me for the last time! ($@)" 1>&2
  fi

  return $exitCode
}

然后将其与任何正确设置失败退出代码的命令结合使用:

with_backoff curl 'http://monkeyfeathers.example.com/'

答案 1 :(得分:6)

也许这会有所帮助。它会尝试命令,如果失败,它会告诉你并暂停,让你有机会修复run-my-script

COMMAND=./run-my-script.sh 
until $COMMAND; do 
    read -p "command failed, fix and hit enter to try again."
done

答案 2 :(得分:0)

我遇到了类似的问题,我需要使用正在启动但尚未启动的curl来与服务器联系,或者由于某种原因暂时不可用的服务。脚本失控了,所以我制作了一个专用的重试工具,该工具将重试命令直到成功:

#there are some files(A,B,C,D,E) in my to_upload directory,
# which I'm trying to upload to my ftp server with curl command
for files in `ls` ;
    do retry curl -f -T $files ftp.myserver.com --user ID:pw ;
done

curl命令具有-f选项,如果由于某种原因而卷曲失败,它将返回代码22。

默认情况下,重试工具将一遍又一遍地运行curl命令,直到该命令返回零状态为止,两次重试之间会退后10秒钟。另外,重试将仅一次从stdin读取,一次仅一次写入stdout,如果命令失败,则将所有stdout写入stderr。

可从此处进行重试:https://github.com/minfrin/retry