【问题标题】:how to re-run the "curl" command automatically when the error occurs发生错误时如何自动重新运行“curl”命令
【发布时间】:2012-01-11 03:56:52
【问题描述】:

有时当我使用 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 所说,我更喜欢不需要确认,因为在脚本运行时我总是在睡觉。 :)

【问题讨论】:

  • 您可以将其设置为运行直到成功,但如果您不解决问题,这将无济于事。
  • 我很困惑,你只是想重新运行失败的 curl 命令直到它们成功,还是整个脚本?
  • 是的,因为大多数时候我重新运行它们几次后它们会成功,所以我希望它们自动重新运行

标签: linux bash curl


【解决方案1】:

这是我用来执行指数回退的 bash sn-p:

# 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/'

【讨论】:

  • 谢谢你的帮助,phs :) 我觉得这是一个很好的脚本,但我是一个linux新手,所以我需要一点时间来理解它。
  • 我希望你能最喜​​欢的答案,因为我相信我稍后会回来寻找这个。
  • @user1076599 刚开始,您可以将其粘贴到脚本顶部附近的某个位置,然后删除 set +eset -e 行。然后你就可以在你的脚本中使用它了。为了更有趣,你可以把它放在你的 shell 的登录脚本中(~/.profile~/.bash_profile~/.bashrc)。
  • "while [[ $attempt
  • 使用attempt=1 精确执行$ATTEMPTS 定义的尝试次数。使用attempts=0,它会额外做一个。见gist.github.com/fernandoacorreia/…
【解决方案2】:

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

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

【讨论】:

  • 你也可以使用 sleep 代替 read -p 这样你就不必每次重试都确认。
  • 阅读的目的是让他有时间解决问题。这需要不确定的时间和互动,所以我认为要求确认不会有问题。
  • 嗨,凯文。我认为你的解决方案真的可以帮助我。
  • 很高兴为您提供帮助。您也可以将它与上面的 phs 函数结合起来运行几次,然后再要求您修复它。
  • 只需将您想要重新运行的所有内容放入一个单独的脚本中,包括 phs 的退避(如果需要),然后像我的示例一样运行该脚本。
【解决方案3】:

我遇到了类似的问题,我需要使用 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 选项,如果 curl 由于某种原因失败,它会返回代码 22。

默认情况下,重试工具将永远反复运行 curl 命令,直到命令返回状态为零,重试之间会退出 10 秒。此外,retry 只会从 stdin 读取一次,并且只会一次写入 stdout,如果命令失败,则将所有 stdout 写入 stderr。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多