让curl 将错误发送到stderr,并将其他所有内容发送到stdout

发布于 2024-11-27 22:59:23 字数 381 浏览 1 评论 0原文

有没有办法告诉curl将错误输出到stderr,并将其他所有内容输出到stdout?

原因是我每天晚上都从命令行使用curl(实际上是一个cronjob)将文件上传到FTP 站点。不幸的是,由于curl 在stderr 上输出状态信息,因此当实际上没有出现任何问题时,我会收到一封有关错误的电子邮件。 (我将 stdout 重定向到日志文件,但保持 stderr 不变,以便 cron 会在有任何输出时通过电子邮件将其发送给我。)

有一些选项可以使 curl 静默,或将所有内容输出到 stdout,但是这两种选择防止错误出现在 stderr 上 - 这意味着当实际上存在我想了解的错误时我不会收到电子邮件。

那么有没有办法让curl只在stderr上输出错误,而在stdout上保持正常输出不变呢?

Is there a way to tell curl to output errors to stderr, and everything else to stdout?

The reason is that I am using curl from the command line (actually a cronjob) to upload a file to an FTP site every evening. Unfortunately because curl outputs status information on stderr, I receive an e-mail about an error when nothing actually went wrong. (I'm redirecting stdout to a log file, but leaving stderr unchanged so that cron will e-mail it to me if there is any output.)

There are options to make curl silent, or output everything to stdout, however both these alternatives prevent errors from appearing on stderr - meaning I won't get an e-mail when there is actually an error I want to know about.

So is there a way to make curl only output errors on stderr, but leave normal output intact on stdout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏の忆 2024-12-04 22:59:23

试试这个:

# No error messages because it succeeds.
curl  http://www.shikadi.net/        --fail --silent --show-error

# This prints an error message to stderr
curl  http://i.like.you.def.maybe/   --fail --silent --show-error

感谢 Russell Davis 在本页上的回答,mancurl,以及反复试验。
出于好奇,这里是问题的 wget 版本:https://superuser.com/questions/420120/wget-is-silent-but-it-displays-error-messages

Try this:

# No error messages because it succeeds.
curl  http://www.shikadi.net/        --fail --silent --show-error

# This prints an error message to stderr
curl  http://i.like.you.def.maybe/   --fail --silent --show-error

Thanks to Russell Davis's answer on this page, man curl, and trial and error.
For the curious, here is the wget version of the question: https://superuser.com/questions/420120/wget-is-silent-but-it-displays-error-messages

苍白女子 2024-12-04 22:59:23

curl -s -S

从手册页:

-s 静默或安静模式。不显示进度表或错误消息。使 Curl 静音。

-S 当与 -s 一起使用时,curl 会在失败时显示错误消息。

curl -s -S

From the man page:

-s Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute.

-S When used with -s it makes curl show an error message if it fails.

轮廓§ 2024-12-04 22:59:23

经过更多实验后,我提出了以下解决方法,但我仍然愿意接受更好的替代方案。

它的工作原理是将所有输出(stdout 和 stderr)临时存储在临时文件中,然后根据curl 的退出代码将该文件的内容发送到 stderr 或 stdout。如果curl失败,整个输出将转到stderr(并通过cron通过电子邮件发送给我),但如果curl成功,输出将转到stdout(在cron命令中重定向到日志文件,导致没有电子邮件。)

# Get a temporary filename
CURL_LOG=`tempfile`

(
  # Run curl, and stick all output in the temp file
  /usr/bin/curl --verbose ... > "$CURL_LOG" 2>&1
) || (
  # If curl exited with a non-zero error code, send its output to stderr so that
  # cron will e-mail it.
  cat "$CURL_LOG" > /dev/stderr
  rm "$CURL_LOG"
  exit 1
)

# Otherwise curl completed successfully, so send the output to stdout (which
# is redirected to a log file in crontab)
cat "$CURL_LOG"
rm "$CURL_LOG"

After some more experimentation I have come up with the following workaround, but I'm still open to better alternatives.

It works by temporarily storing all output (stdout and stderr) in a temporary file, and then sending the contents of that file to stderr or stdout depending on curl's exit code. If curl failed the entire output will go to stderr (and be e-mailed to me thanks to cron), but if curl succeeded the output will go to stdout instead (which is redirected to a log file in the cron command, resulting in no e-mail.)

# Get a temporary filename
CURL_LOG=`tempfile`

(
  # Run curl, and stick all output in the temp file
  /usr/bin/curl --verbose ... > "$CURL_LOG" 2>&1
) || (
  # If curl exited with a non-zero error code, send its output to stderr so that
  # cron will e-mail it.
  cat "$CURL_LOG" > /dev/stderr
  rm "$CURL_LOG"
  exit 1
)

# Otherwise curl completed successfully, so send the output to stdout (which
# is redirected to a log file in crontab)
cat "$CURL_LOG"
rm "$CURL_LOG"
海螺姑娘 2024-12-04 22:59:23

curl -f。文档说“服务器错误时静默失败(根本没有输出)”,但它实际上意味着“没有输出到标准输出”——错误仍然会输出到标准错误。

curl -f. The documentation says "Fail silently (no output at all) on server errors" but it really means "no output to stdout" -- errors will still be output to stderr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文