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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8350942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:40:24  来源:igfitidea点击:

how to re-run the "curl" command automatically when the error occurs

linuxbashcurl

提问by erical

Sometimes when I execute a bash script with the curlcommand to upload some files to my ftp server, it will return some error like:

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

56 response reading failed

and I have to find the wrong line and re-run them manually and it will be OK.

我必须找到错误的行并手动重新运行它们,它会没问题。

I'm wondering if that could be re-run automatically when the error occurs.

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



My scripts is like this:

我的脚本是这样的:

#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

But sometimes A,B,C, would be uploaded successfully, only D were left with an "error 56", so I have to rerun curl command manually. Besides, as Will Bickford said, I prefer that no confirmation will be required, because I'm always asleep at the time the script is running. :)

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

回答by Kevin

Perhaps this will help. It will try the command, and if it fails, it will tell you and pause, giving you a chance to fix run-my-script.

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

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

回答by phs

Here's a bash snippet I use to perform exponential back-off:

这是我用来执行指数退避的 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
}

Then use it in conjunction with any command that properly sets a failing exit code:

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

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

回答by Graham Leggett

I have faced a similar problem where I need to make contact with servers using curl that are in the process of starting up and haven't started up yet, or services that are temporarily unavailable for whatever reason. The scripting was getting out of hand, so I made a dedicated retry tool that will retry a command until it succeeds:

我遇到了类似的问题,我需要使用 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

The curl command has the -f option, which returns code 22 if the curl fails for whatever reason.

curl 命令有 -f 选项,如果 curl 因任何原因失败,它会返回代码 22。

The retry tool will by default run the curl command over and over forever until the command returns status zero, backing off for 10 seconds between retries. In addition retry will read from stdin once and once only, and writes to stdout once and once only, and writes all stdout to stderr if the command fails.

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

Retry is available from here: https://github.com/minfrin/retry

可以从这里重试:https: //github.com/minfrin/retry