Make curl use stderr properly
May 30, 2025
daybook, shell, curl | permalink
At work, there was a cronjob that used curl, and it was sending an email every time it ran, which in theory it shouldn't have because I was redirecting the stdout from the curl call to /dev/null with > /dev/null. But it did, so after the internet failed to deliver me a good answer, I took a peek into man curl.
What was happening is that curl reports the progress of a request to stderr. Why? I don't know. Stderr is weird. I wanted only actual errors to be emailed to me. So I turned off progress reporting with --no-progress-meter. But if I tried a url that returned a 404, that was also getting binned into /dev/null, e.g. curl --no-progress-meter https://kdwarn.net/api/no-api-here > /dev/null returned nothing, meaning there was nothing getting reported to stderr. Then I found the --fail option, which makes curl report error 22 (and the http error) to stderr, so now I get only the error reported to stderr with curl --fail --no-progress-meter https://kdwarn.net/api/no-api-here > /dev/null. I tested binning stderr to make sure I wasn't insane (because the curl documentation on this isn't very clear), and in fact curl --fail --no-progress-meter https://kdwarn.net/api/no-api-here 2> /dev/null prints no output.