brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl -w %output{} appends a log line to a file for every request

Curl
~/tips/tip-curl-write-out-output-file

curl -s -o /dev/null \

Write the status line of each check to a log file, while the body goes elsewhere.

When you log a health check, stdout is usually redirected, and then the body and the status mix. With %output{} the write-out text goes straight into a file of its own.

Put the instruction at the start of the format, with >> to append:

curl -s -o /dev/null \
  -w '%output{>>checks.log}%{time_total}s %{http_code} %{url_effective}\n' \
  https://example.com/health

Each run adds one line like 0.21s 200 https://example.com/health to checks.log. Without the >>, curl replaces the file every time. Everything after the instruction goes to that file; text before it still goes to standard output.

There is a related variable for errors. %{stderr} sends the rest of the write-out output to standard error, which keeps a value out of a pipe that expects only data.

A few limits I’d keep in mind: this needs curl 8.3 or newer, and the man page says that if the file cannot be created, curl keeps writing to the previous destination without changing its exit code. So a typo in the folder name does not fail the command; you just find the lines on your terminal. The line is written even when the transfer fails, which is what you want for a log, but check the exit code separately if a script has to react.

Further reading: curl man page, everything curl: write out

Going deeper: Keep a domain pointed at your home with Cloudflare DDNS on Unraid

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link