Web servers and networking · Intermediate · 17 min
Make Let's Encrypt renewals reliable with Certbot hooks and expiry checks
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Certbot 5.10 · Nginx 1.30
You will have Certbot 5.10 renewals that reload Nginx, hand fresh certificates to a second service with safe permissions, and an expiry check that alerts you on a daily timer when a renewal has quietly failed.
On this page
Getting the first certificate is the easy part. A renewal can fail months later, at 03:00, with nobody watching: port 80 got closed, a DNS change went out, or the certificate renewed but the service using it never reloaded and kept serving the old one. Let's Encrypt stopped sending expiry reminder emails in June 2025, so nobody warns you by default.
Reliable renewal comes down to three things, and this tutorial covers each one:
- the scheduled job runs, and you know where to look when it fails
- every program that uses the certificate picks up the new one
- you hear about it while there is still time to fix things
You start from the server in the free HTTPS tutorial: Nginx, and a Certbot certificate for example.com and www.example.com. Replace example.com with your own domain throughout.
Step 1 · Find the job that renews your certificates
The Certbot snap ships its own scheduled task. snapd turns it into a systemd timer called snap.certbot.renew.timer, which runs certbot -q renew about twice a day at a random time. The random time spreads the load on Let's Encrypt. Running twice a day is harmless, because renew only acts on certificates that are due.
Look at the timer and when it fires next:
systemctl list-timers snap.certbot.renew.timerThen read what the last few runs did. The service is quiet when nothing is due, so an empty or short log is normal:
journalctl -u snap.certbot.renew.service --since "-3 days" --no-pagerCertbot also keeps its own detailed log of every run in /var/log/letsencrypt/letsencrypt.log, and keeps a few older runs alongside it as letsencrypt.log.1, .2 and so on. When a renewal fails, that file names the reason.
Check it worked
list-timers prints one line for snap.certbot.renew.timer, with a NEXT time within the next day and a LAST time within about the past day. The two daily runs fall at random times, so they can be almost 24 hours apart. If the line is missing, the snap's timer is not active, and nothing renews your certificate. Reinstall Certbot as in the free HTTPS tutorial before you go on.
Step 2 · Read what Certbot knows about the certificate
Certbot keeps every certificate as a lineage: a name (usually the first domain), a folder of versions, and a renewal file that records how the certificate was obtained. List them:
sudo certbot certificatesFor each certificate you get its name, serial number, the names it covers, the expiry date with the days left (VALID: 89 days, for example) and the paths to the files.
Now look at the renewal file. This is what certbot renew reads on every run:
sudo ls /etc/letsencrypt/renewal/
sudo cat /etc/letsencrypt/renewal/example.com.confThe top lines point at the current files in /etc/letsencrypt/live/example.com/. The [renewalparams] section holds the account, the server (the Let's Encrypt production API, https://acme-v02.api.letsencrypt.org/directory) and the authenticator, which is nginx if you used certbot --nginx. On renewal Certbot repeats that method, so whatever worked the first time has to keep working.
sudo ls -l /etc/letsencrypt/live/example.com/The files in live/ are symbolic links into /etc/letsencrypt/archive/example.com/, where each renewal adds a numbered set (cert1.pem, cert2.pem...). Nginx reads the live/ paths, so it always sees the newest version after a reload. Do not move, copy over or edit these files by hand. To change how a certificate renews (a new webroot path, for example), use sudo certbot reconfigure --cert-name example.com with the new options. It tests them against the staging server before saving them.
Check it worked
certbot certificates shows example.com with both names and VALID, and the renewal file has a [renewalparams] section with a server = line. If the days left are below 30 for a 90-day certificate, a renewal has already been missed. Read /var/log/letsencrypt/letsencrypt.log before going further.
Step 3 · Reload Nginx with a deploy hook
Certbot runs three kinds of hook during renewal:
- pre hooks run before it tries to renew
- post hooks run after every attempt, whether it worked or not
- deploy hooks run once for each certificate that was actually renewed
A deploy hook is the right place to tell programs about the new certificate. Any executable file in /etc/letsencrypt/renewal-hooks/deploy/ runs as a deploy hook for every certificate, in the byte order of the file names. A number at the start of each name makes that order obvious.
If you used certbot --nginx, Certbot already reloads Nginx after a renewal. A hook of your own still helps: it also covers certificates obtained with certonly, which reloads nothing, and it checks the configuration first, so a half-finished edit somewhere else is never loaded.
sudo tee /etc/letsencrypt/renewal-hooks/deploy/10-reload-nginx > /dev/null <<'HOOK'
#!/bin/sh
# Reload Nginx after a certificate is renewed.
# nginx -t stops the hook if the configuration does not parse.
set -eu
nginx -t -q
systemctl reload nginx
HOOK
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/10-reload-nginxA reload keeps open connections alive and makes new connections use the new certificate. A restart is never needed for this.
Check it worked
sudo ls -l /etc/letsencrypt/renewal-hooks/deploy/
sudo sh -n /etc/letsencrypt/renewal-hooks/deploy/10-reload-nginx && echo "syntax OK"The file shows as -rwxr-xr-x and owned by root, and the syntax check prints syntax OK. A hook without the execute bit is skipped without an error, so the permissions matter.
Step 4 · Copy the certificate to a service that is not Nginx
Other programs often need the same certificate: an MQTT broker, a mail server, or an app that serves TLS itself. Most of them drop root and run as their own user, and that user cannot read /etc/letsencrypt. Certbot creates live/ and archive/ with mode 0700, and private keys with mode 0600, both owned by root.
Do not loosen permissions on /etc/letsencrypt. Give the service its own copy instead, refreshed by a deploy hook, readable by its group only.
This tutorial uses a stand-in system user called tlsdemo in place of your service. Create it:
sudo useradd --system --user-group --no-create-home --shell /usr/sbin/nologin tlsdemoNow write the hook. Certbot sets two variables for deploy hooks: RENEWED_LINEAGE is the certificate's live/ folder, and RENEWED_DOMAINS lists its names, separated by spaces. The hook acts only for the example.com lineage, so other certificates on the server are left alone.
sudo tee /etc/letsencrypt/renewal-hooks/deploy/20-copy-to-tlsdemo > /dev/null <<'HOOK'
#!/bin/sh
# Give the tlsdemo service its own copy of the example.com certificate.
set -eu
[ "$RENEWED_LINEAGE" = "/etc/letsencrypt/live/example.com" ] || exit 0
dest=/etc/tlsdemo/tls
group=tlsdemo
install -d -m 750 -o root -g "$group" "$dest"
# install creates each file with its final owner and mode, so the key is never readable by others.
install -m 644 -o root -g "$group" "$RENEWED_LINEAGE/fullchain.pem" "$dest/fullchain.pem.new"
install -m 640 -o root -g "$group" "$RENEWED_LINEAGE/privkey.pem" "$dest/privkey.pem.new"
# Rename into place, so the service never reads a half-written file.
mv -f "$dest/fullchain.pem.new" "$dest/fullchain.pem"
mv -f "$dest/privkey.pem.new" "$dest/privkey.pem"
if systemctl is-active --quiet tlsdemo.service; then
systemctl restart tlsdemo.service
fi
HOOK
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/20-copy-to-tlsdemoFor your own service, change tlsdemo in the group, the folder and the unit name. Point the service's configuration at /etc/tlsdemo/tls/fullchain.pem and /etc/tlsdemo/tls/privkey.pem (or your own folder). If the service can reload its certificate without a restart, use systemctl reload instead. The is-active test means the hook does not fail when the service is stopped.
Check it worked
sudo sh -n /etc/letsencrypt/renewal-hooks/deploy/20-copy-to-tlsdemo && echo "syntax OK"
getent group tlsdemoYou see syntax OK and a tlsdemo group line. The folder does not exist yet. The first run of the hook creates it, in the next step.
Step 5 · Rehearse a renewal with --dry-run
A dry run goes through a full renewal against the Let's Encrypt staging server, which has much higher rate limits, and throws the test certificate away. Your real certificate is not touched:
sudo certbot renew --dry-runThe output ends with Congratulations, all simulated renewals succeeded. If it fails, the error is the one the timer would hit too, so fix it now.
A plain dry run does not run deploy hooks, so it does not prove your hooks work. Add --run-deploy-hooks. After a successful dry run, Certbot then runs the hooks against your current, real certificate:
sudo certbot renew --dry-run --run-deploy-hooksRun this every time you change a hook.
Check it worked
Look at the copy the hook made:
sudo stat -c '%a %U:%G %n' /etc/tlsdemo/tls /etc/tlsdemo/tls/fullchain.pem /etc/tlsdemo/tls/privkey.pem
sudo cmp /etc/letsencrypt/live/example.com/fullchain.pem /etc/tlsdemo/tls/fullchain.pem && echo "copy matches"You want 750 root:tlsdemo for the folder, 644 root:tlsdemo for the chain, 640 root:tlsdemo for the key, and copy matches. Then confirm the service user can read the key:
sudo -u tlsdemo openssl pkey -in /etc/tlsdemo/tls/privkey.pem -noout && echo "tlsdemo can read the key"Step 6 · Renew once for real and watch the hooks fire
A dry run tests the CA side. One real renewal proves the whole chain: new certificate, Nginx reloaded, copy refreshed and Nginx serving the new serial number.
Let's Encrypt limits how often you can do this. Up to 5 certificates for the exact same set of names can be issued every 7 days, and up to 50 per registered domain. Five failed validations for one name in an hour also blocks new orders for a while. Run the forced renewal below once, not in a loop, and never put --force-renewal in a scheduled job.
Note the current serial number first:
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -serial -enddateThen renew this one certificate now, even though it is not due:
sudo certbot renew --cert-name example.com --force-renewalCertbot names each deploy hook as it runs it. Now compare three serial numbers: the new file, the copy, and the certificate Nginx sends to a visitor.
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -serial
sudo openssl x509 -in /etc/tlsdemo/tls/fullchain.pem -noout -serial
openssl s_client -connect example.com:443 -servername example.com < /dev/null 2> /dev/null | openssl x509 -noout -serial -enddateHow Let's Encrypt decides when to renew
Now run what the timer runs:
sudo certbot renewIt reports that the certificate is not yet due, and that no renewals were attempted. Here is how Certbot decides when it becomes due.
Certbot 4.1 and later first ask the CA through ACME Renewal Information (ARI). Let's Encrypt replies with a suggested renewal window, around two thirds of the way through the lifetime. When the CA answers, its suggested window decides; if it does not (and you have not set renew_before_expiry), Certbot falls back to renewing when a third of the lifetime is left, about 30 days before expiry for a 90-day certificate. If the CA needs to replace certificates early, for example after a mass revocation, it moves the window forward and the next timer run renews on its own. Renewals made inside an ARI window are exempt from all Let's Encrypt rate limits. All of this needs the timer to keep running.
Certbot records when it may ask again:
sudo grep -A1 'acme_renewal_info' /etc/letsencrypt/renewal/example.com.confCheck it worked
All three serial numbers match each other and differ from the one you noted before, and the expiry date is about 90 days away. certbot renew says the certificate is not due, and the renewal file now has an [acme_renewal_info] section with an ari_retry_after time. If the served serial is still the old one, Nginx did not reload. Run sudo certbot renew --dry-run --run-deploy-hooks and read the hook output.
Step 7 · Write an expiry check that alerts you
Hooks cover the case where renewal works. You also need to know when it does not. The check below connects to your site the way a visitor does and reads the certificate Nginx actually serves. That catches a failed renewal, and also a renewal that worked while Nginx kept the old certificate. The openssl s_client pipe and -checkend test inside it are explained in Understand TLS certificates and chains with OpenSSL; what is new here is the alert, the private settings file and the daily timer.
When it finds a problem, the script prints it, exits with an error and, if you have set a webhook URL, posts a short JSON message to it. Slack and Google Chat incoming webhooks accept a {"text": "..."} body like this one. Other tools, Microsoft Teams Workflows webhooks included, expect a different JSON shape, so point the script at a small relay that reshapes the message for them. Many teams in Nigeria and across Africa follow alerts in a Telegram or WhatsApp group rather than email. Point the webhook at a relay that posts there; the script only needs a URL that accepts JSON.
sudo tee /usr/local/bin/check-cert-expiry > /dev/null <<'SCRIPT'
#!/bin/sh
# Warn when the certificate a site serves is close to expiry.
# Usage: check-cert-expiry HOSTNAME [DAYS] (DAYS defaults to 14)
set -eu
host="$1"
days="${2:-14}"
ALERT_WEBHOOK_URL=""
if [ -r /etc/cert-expiry-check.env ]; then
. /etc/cert-expiry-check.env
fi
alert() {
echo "$1"
if [ -n "$ALERT_WEBHOOK_URL" ]; then
curl -fsS -m 10 -H 'Content-Type: application/json' \
--data "{\"text\": \"$1\"}" "$ALERT_WEBHOOK_URL" > /dev/null || echo "could not send the alert"
fi
}
cert=$(openssl s_client -connect "$host:443" -servername "$host" < /dev/null 2> /dev/null \
| openssl x509 2> /dev/null) || true
if [ -z "$cert" ]; then
alert "CRITICAL: no certificate from $host:443"
exit 2
fi
end=$(printf '%s\n' "$cert" | openssl x509 -noout -enddate | cut -d= -f2)
if printf '%s\n' "$cert" | openssl x509 -noout -checkend $((days * 86400)) > /dev/null; then
echo "OK: $host certificate valid until $end"
else
alert "CRITICAL: $host certificate expires within $days days (not after $end)"
exit 1
fi
SCRIPT
sudo chmod 755 /usr/local/bin/check-cert-expiryThe webhook URL works like a password, because anyone who has it can post to your channel. Keep it out of the script and out of your shell history. Create the settings file private, owned by root and readable by root only, before anything goes in it:
sudo install -m 600 -o root -g root /dev/null /etc/cert-expiry-check.envOpen it in an editor and add one line with your URL, in single quotes (replace the example URL):
sudoedit /etc/cert-expiry-check.envALERT_WEBHOOK_URL='https://hooks.example.net/replace-this'Choose the number of days with the renewal schedule in mind. For a 90-day certificate, Certbot renews with about 30 days left, so an alert at 14 days means renewals have been failing for two weeks and there is still time to act. Let's Encrypt is shortening lifetimes, to 64 days from February 2027 and 45 days from February 2028. With 45-day certificates renewal happens with about 15 days left, so lower the threshold to 7 days then.
Check it worked
Run the check normally:
sudo /usr/local/bin/check-cert-expiry example.comIt prints OK: example.com certificate valid until and the date. Then pretend the threshold is 100 days, longer than any Let's Encrypt certificate lasts, to see a failure and an alert:
sudo /usr/local/bin/check-cert-expiry example.com 100 || echo "exit code $?"It prints a CRITICAL line and exit code 1, and the message appears in your channel. If the message does not arrive, the script prints could not send the alert. Check the URL in /etc/cert-expiry-check.env.
Step 8 · Run the check every day with a systemd timer
A small systemd service runs the check, and a timer starts it once a day. When the check fails, the service is marked as failed, so it also shows in systemctl --failed and in any monitoring that watches failed units.
sudo tee /etc/systemd/system/cert-expiry-check.service > /dev/null <<'UNIT'
[Unit]
Description=Check the TLS certificate example.com serves
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-cert-expiry example.com 14
UNIT
sudo tee /etc/systemd/system/cert-expiry-check.timer > /dev/null <<'UNIT'
[Unit]
Description=Daily TLS certificate expiry check
[Timer]
OnCalendar=*-*-* 07:00:00
RandomizedDelaySec=30m
Persistent=true
[Install]
WantedBy=timers.target
UNIT
sudo systemd-analyze verify /etc/systemd/system/cert-expiry-check.service /etc/systemd/system/cert-expiry-check.timersystemd-analyze verify prints nothing when both files are valid. Persistent=true runs a missed check after the server was switched off at 07:00. To check several sites, add one ExecStart= line per host to the service. With Type=oneshot they run in order, and the service fails if any of them fails.
Load the units, start the timer, and run the check once by hand:
sudo systemctl daemon-reload
sudo systemctl enable --now cert-expiry-check.timer
sudo systemctl start cert-expiry-check.service
journalctl -u cert-expiry-check.service -n 5 --no-pagerCheck it worked
systemctl list-timers cert-expiry-check.timerThe timer is listed with a NEXT time tomorrow morning, and the journal shows the OK: line from the run you started. You now have both sides covered: Certbot renews about twice a day and passes the new certificate on, and a separate daily check tells you when it did not.
Remove the stand-in service
If tlsdemo was only for practice, remove the copy hook first, so the next renewal does not recreate the folder, then remove the folder and the user. Check the paths before you press Enter, because rm -r does not ask:
sudo rm /etc/letsencrypt/renewal-hooks/deploy/20-copy-to-tlsdemo
sudo rm -r /etc/tlsdemo
sudo userdel tlsdemoKeep 10-reload-nginx, the expiry check and its timer.
Next steps
- Understand what is inside the files Certbot writes, and check a chain by hand: /tutorials/understand-tls-certificates-and-chains-with-openssl
- Get a certificate for a server that port 80 cannot reach, or for a wildcard name: /tutorials/get-a-wildcard-certificate-with-a-dns-01-challenge
- Learn more about timers like the expiry check: /tutorials/replace-cron-jobs-with-systemd-timers
- Go back to the first certificate setup: /tutorials/free-https-with-lets-encrypt-and-nginx
Related tutorials
- Write Nginx location blocks that match what you expectYou will build a practice Nginx 1.30 server where every location block labels its responses, predict which block answers each of twelve URLs, check your answers with curl, and fix the alias, regex-order and proxy_pass mistakes that catch most people out.
- Write a server firewall with nftablesYou will write one /etc/nftables.conf with a default-drop input chain, rate-limited SSH, named address sets, counters and logging, and load it over SSH behind a timer that undoes it if you lose your connection.
- Watch a TCP connection open and close with tcpdumpYou will capture a curl request to a local web server with tcpdump on Ubuntu 26.04, read its handshake, data and teardown line by line, see the reset a closed port sends, and save, filter and rotate captures as pcap files.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials