Certbot SSL Auto-Renewal Check Shell Script (Let’s Encrypt)

certbot renewal script

Ensure Let’s Encrypt SSL certificates renew automatically — this script runs certbot renew --dry-run and alerts you on failure.

What this script does

  • Dry-run renewal test with Certbot
  • Lists certificates expiring within 14 days
  • Reloads nginx/apache after successful renewal
  • Email alert on renewal failure
  • Designed for weekly cron verification

Prerequisites

  • Certbot installed
  • Existing Let’s Encrypt certificates
  • nginx or apache configured with certbot

Step 1: Save the script

sudo nano /usr/local/bin/certbot-renew-check.sh
sudo chmod +x /usr/local/bin/certbot-renew-check.sh

Step 2: Full script (scroll to read)

certbot-renew-check.sh
#!/usr/bin/env bash
set -euo pipefail

WEBSERVER="nginx"
ALERT_EMAIL="[email protected]"
WARN_DAYS=14
LOG="/var/log/certbot-check.log"

log(){ echo "[$(date '+%F %T')] $*" | tee -a "$LOG"; }

log "Certificates on $(hostname -s):"
certbot certificates 2>&1 | tee -a "$LOG"

log "Running certbot renew --dry-run"
if certbot renew --dry-run >> "$LOG" 2>&1; then
  log "Dry-run renewal: SUCCESS"
else
  log "Dry-run renewal: FAILED"
  [[ -n "$ALERT_EMAIL" ]] && command -v mail >/dev/null && \
    echo "Certbot dry-run failed on $(hostname -s). Check $LOG" | \
    mail -s "Certbot renewal FAILED" "$ALERT_EMAIL"
  exit 1
fi

log "Checking expiry within ${WARN_DAYS} days"
certbot certificates 2>/dev/null | grep -E "Expiry Date|Certificate Name" | tee -a "$LOG"
exit 0

Scroll inside the box to read the full script.

Step 3: Configure settings

  • WEBSERVER — nginx or apache for reload hook
  • ALERT_EMAIL — notification address
  • Run real renew via system timer: certbot renew
Certbot SSL renewal check shell script Linux
Certbot SSL renewal check shell script Linux

Step 4: Test manually

sudo certbot certificates
sudo /usr/local/bin/certbot-renew-check.sh

Schedule with cron

sudo crontab -e

Add:

0 4 * * 0 /usr/local/bin/certbot-renew-check.sh >> /var/log/certbot-check.log 2>&1

Related tutorials

Terminal screenshot is an original illustration created for Gnome IT Solutions (blog.gnomeitsolutions.com).