让curl 将错误发送到stderr,并将其他所有内容发送到stdout
有没有办法告诉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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
感谢 Russell Davis 在本页上的回答,
mancurl
,以及反复试验。出于好奇,这里是问题的
wget
版本:https://superuser.com/questions/420120/wget-is-silent-but-it-displays-error-messagesTry this:
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-messagescurl -s -S
从手册页:
curl -s -S
From the man page:
经过更多实验后,我提出了以下解决方法,但我仍然愿意接受更好的替代方案。
它的工作原理是将所有输出(stdout 和 stderr)临时存储在临时文件中,然后根据curl 的退出代码将该文件的内容发送到 stderr 或 stdout。如果curl失败,整个输出将转到stderr(并通过cron通过电子邮件发送给我),但如果curl成功,输出将转到stdout(在cron命令中重定向到日志文件,导致没有电子邮件。)
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.)
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.