49

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?

Malvineous
  • 25,144
  • 16
  • 116
  • 151

4 Answers4

60

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

Community
  • 1
  • 1
dgo.a
  • 2,634
  • 23
  • 35
  • Won't `--silent` prevent normal progress messages from being sent to stdout though? I would still like to see these so that I know curl is actually doing something! – Malvineous Aug 17 '13 at 06:38
  • 1
    There is also another problem with my answer: I've never used curl for FTP upload. This has been a waste of your time. I apologize for overlooking important aspects of your question. I will delete this answer to avoid further confusion. – dgo.a Aug 17 '13 at 08:56
  • Don't worry about it - it might help someone else just as Russell Davis' answer gave you some ideas :-) – Malvineous Aug 24 '13 at 11:40
  • These are the default args I use every time with curl. – Alex Jan 03 '23 at 07:07
21

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.

Community
  • 1
  • 1
Timmah
  • 2,001
  • 19
  • 18
  • 2
    Have a look at the comments under @dgo.a's answer - this also removes the normal progress messages that tell you whether things are working as expected, which I want to keep on stdout. – Malvineous Dec 22 '16 at 08:18
6

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"
Malvineous
  • 25,144
  • 16
  • 116
  • 151
  • 3
    It's not short and sweet like the other answers, but it's the only answer that actually gets the job done. Thanks for sharing. – 7yl4r Mar 07 '17 at 19:54
1

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.

Russell Davis
  • 8,319
  • 4
  • 40
  • 41