Know when certbot renewals stop succeeding, weeks before the certificate expires

Updated 4 min read

On this page
  1. The problem
  2. What goes wrong silently
  3. Setup
  4. Recommended settings
  5. Related
  6. FAQ

Each night's certbot renew tells Stallbell how it went, and Stallbell emails you when it fails two nights running, hangs, or doesn't run at all.

The problem

certbot renew runs from a timer or cron, and nobody reads its output. Then a web server config change breaks the HTTP challenge, a firewall rule blocks port 80, or the DNS plugin's token expires. Every night's renewal fails, quietly, and the certificate still works, until the day it expires and browsers show a full-page warning.

This page is a ready-made setup for one scenario.

What goes wrong silently

What Stallbell catches
What happensWhat Stallbell seesAlert
A renewal fails two nights in a row (challenge fails, CA unreachable, plugin error)Two fail pings with certbot's first error lineFailure streak
Another certbot is already running and holds the lock, two nights runningTwo fail pings with "Another instance of Certbot is already running."Failure streak
certbot hangsA start ping with no finish within 15 minutesStuck run
The cron line is gone after a rebuild, or the server is offNo ping by 03:17 plus graceMissed run
certbot isn't on cron's PATHExit code 127, two nights runningFailure streak
The next run succeedsA good finish after the incidentRecovered
Apply this template

Creates a monitor called certbot-renewal-job with the settings below. You can change everything later.

Setup

Find where certbot lives with command -v certbot (it's often /usr/bin/certbot, or /snap/bin/certbot for the snap) and use that full path below. Then save this as /usr/local/bin/certbot-renew-report and make it executable:

Shell
#!/bin/sh
# certbot-renew-report: run certbot renew and tell Stallbell how it went, with certbot's first error line.
URL="https://stallbell.com/p/TOKEN"

curl -fsS -m 10 --retry 3 -o /dev/null "$URL/start"
out=$(/usr/bin/certbot renew -q 2>&1)
code=$?
[ -n "$out" ] && printf '%s\n' "$out" >&2
curl -fsS -m 10 --retry 3 -o /dev/null -G "$URL/$code" \
  --data-urlencode "msg=$(printf '%s' "$out" | head -n 1 | cut -c 1-500)"
exit "$code"

Add it to root's crontab (sudo crontab -e):

crontab
17 3 * * * /usr/local/bin/certbot-renew-report

With -q, certbot prints only errors. The script sends the first error line as msg, so the alert email's Failure line reads, for example, "Failed to renew certificate example.org with error: …". It also passes certbot's output and exit code through, so cron's own mail still works.

If you'd rather not add a script, this crontab line sends the start and the exit code, without the message:

crontab
17 3 * * * curl -fsS -m 10 --retry 3 -o /dev/null https://stallbell.com/p/TOKEN/start; /usr/bin/certbot renew -q; curl -fsS -m 10 --retry 3 -o /dev/null https://stallbell.com/p/TOKEN/$?

certbot renew exits 1 only when a renewal attempt failed (certbot's docs). A night with nothing due exits 0, which is a good run.

Already have a timer? Most installs run certbot renew from a systemd timer or a cron entry already (check with systemctl list-timers). Leave it in place. This line is an extra nightly run that reports. If the two ever overlap, the second one exits 1 with "Another instance of Certbot is already running.", which is one reason the template waits for two failures in a row.

Recommended settings
FieldValueWhy
KindScheduled jobA label only. certbot renew runs from cron.
How it runsOn a cron scheduleThe renewal check runs at a set clock time each night.
Cron expression17 3 * * *03:17 every night, an off-peak minute. Change it to match your crontab line.
Schedule timezoneYour account timezoneFollows your account timezone. Pick your server's zone if its cron runs on a different one.
Grace period1 hour1 hour. A late renewal check isn't urgent; a missing one is.
Failures in a row2Certbot starts renewing well before expiry, so one failed night isn't urgent. Two in a row is.
Stuck after15 minutes15 minutes. A renewal takes seconds to a minute or two, so longer means it hung.

With Failures in a row at 2, the first failed night opens nothing. The second sends a Failure streak email listing the messages from both nights.

How much warning you get. Since certbot 4.0, a certificate is due for renewal when less than a third of its lifetime is left: 30 days for a 90-day certificate (certbot docs). So a renewal that starts failing usually gives you weeks of nightly failures, and two of them are enough to alert. Shorter certificates give less time, but the alert still comes on the second failed night.

FAQ

Does Stallbell check the certificate or its expiry date?

No. Stallbell has no HTTP or SSL checks. It knows whether the renewal job ran and how it exited. That's usually weeks of warning, because certbot renews long before expiry, but it isn't a certificate check.

Why alert after two failures and not one?

certbot renews well before expiry, so one failed night isn't urgent and is often a passing network or rate-limit problem. Two in a row is worth a look.

My certbot already runs from a systemd timer. Do I still need this?

The timer runs certbot, but nobody hears how it went. Keep it, and add this cron line as an extra run that reports. certbot renew only renews certificates that are due, so running it more than once a day is safe.

Why is the stuck time 15 minutes when a renewal takes seconds?

When certbot runs non-interactively and a certificate is due, it waits a random delay before renewing. We saw delays of 8, 223 and 396 seconds. 15 minutes leaves room for that.