使 curl 将错误发送到 stderr 并将其他所有内容发送到 stdout [英] Making curl send errors to stderr and everything else to stdout

查看:27
本文介绍了使 curl 将错误发送到 stderr 并将其他所有内容发送到 stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有一些选项可以让 curl 静音,或者将所有内容输出到标准输出,但是这两种选择都可以防止错误出现在标准错误上——这意味着当我真正想知道错误时,我不会收到电子邮件.

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

解决方案

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

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

<前># 获取临时文件名CURL_LOG=`临时文件`(# 运行 curl,并将所有输出粘贴到临时文件中/usr/bin/curl --verbose ... > "$CURL_LOG" 2>&1) ||(# 如果 curl 以非零错误代码退出,则将其输出发送到 stderr 以便# cron 将通过电子邮件发送给它.猫$CURL_LOG">/dev/stderrrm "$CURL_LOG"出口 1)# 否则 curl 成功完成,因此将输出发送到 stdout(其中# 重定向到 crontab 中的日志文件)猫$CURL_LOG"rm "$CURL_LOG"

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?

解决方案

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"

这篇关于使 curl 将错误发送到 stderr 并将其他所有内容发送到 stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆