Back to Learning Center
Monitoring

How to Check SSL Certificate Expiry for Many Domains with OpenSSL

7 min read·Updated November 2026·By CertNotify Team

To check when the SSL certificates on many domains expire, connect to each one with openssl s_client, read the certificate it serves, and test its end date with openssl x509 -checkend. The script below does that for a list of domains and flags any certificate expiring within 30 days. It needs nothing but openssl and a POSIX shell.

One domain

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate

Output, for example.com on 30 November 2026:

notAfter=Feb 22 23:53:08 2027 GMT
  • echo | closes the connection once the handshake is done, so the command does not wait for input.
  • -servername sets the TLS Server Name Indication, so a server hosting several sites returns the certificate for this one. OpenSSL 1.1.1 and later fill it in from -connect anyway; setting it keeps the command working on older versions.
  • openssl x509 -noout -enddate prints only the certificate's notAfter date.

Is it expiring soon?

-checkend takes a number of seconds. In the words of the OpenSSL manual, it "checks if the certificate expires within the next arg seconds and exits nonzero if yes it will expire or zero if not." 30 days is 2,592,000 seconds:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -checkend 2592000

This prints Certificate will not expire and exits 0. Against a certificate that has already expired, such as the test host expired.badssl.com, it prints Certificate will expire and exits 1.

Every domain in a list

Put one hostname per line in domains.txt, then run the script with the list and a number of days:

#!/bin/sh
# Usage: sh check-ssl-expiry.sh domains.txt [days]
# Prints each host's certificate expiry date and flags any certificate
# that expires within [days] (default 30), or has already expired.
days=${2:-30}
while read -r host; do
  [ -z "$host" ] && continue
  cert=$(echo | openssl s_client -connect "$host:443" -servername "$host" 2>/dev/null | openssl x509 2>/dev/null)
  if [ -z "$cert" ]; then
    echo "$host  ERROR: no certificate read"
    continue
  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
    status="ok"
  else
    status="EXPIRES WITHIN $days DAYS"
  fi
  echo "$host  $end  $status"
done < "$1"
sh check-ssl-expiry.sh domains.txt 30

Output from a test list on 30 November 2026:

example.com  Feb 22 23:53:08 2027 GMT  ok
www.certnotify.com  Jan 17 18:24:31 2027 GMT  ok
expired.badssl.com  Apr 12 23:59:59 2015 GMT  EXPIRES WITHIN 30 DAYS
no-such-host.invalid  ERROR: no certificate read

The certificate is read once per host and tested twice, so each host costs one connection. A host that does not answer is reported as an error rather than skipped. To see only the lines that need attention, filter out the rest: sh check-ssl-expiry.sh domains.txt 30 | grep -v ' ok$'.

What a script does not cover

  • Someone has to run it. A cron job or CI schedule runs it, and something has to notice when that job stops.
  • It checks one server. Behind a load balancer each connection may reach a different backend, and only one is checked.
  • It checks expiry only. A missing intermediate or a certificate for the wrong hostname passes. The free certificate chain checker shows those.
  • It does not check the domain. Registration expiry is a separate date. See how to check a domain's expiry date.
  • Port 443 only. Change :443 for services on other ports.

Certificates are also getting shorter-lived: the CA/Browser Forum schedule takes public certificates to 47 days by 2029 (see the validity timeline). A check that runs daily still gives weeks of warning at that length; a check someone remembers to run does not.

The hosted version

CertNotify runs the same check once a day for every domain you add, along with the domain's registration expiry, and keeps sending an alert each day once a date is inside your lead time. Free covers 3 domains with an email 30 days ahead; Pro covers 25 and adds Slack, Teams, Discord and webhook alerts. If you manage client sites, see monitoring for agencies.

Common questions

How do I check an SSL certificate expiry date from the command line?

Run: echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -enddate. It prints the certificate's notAfter date, the last moment it is valid.

What does openssl x509 -checkend do?

It checks whether the certificate expires within the given number of seconds. It exits with status 1 if it will, and 0 if it will not, so a script can branch on it. 2592000 seconds is 30 days.

Why does the script show a different certificate from my browser?

Usually because the name sent in SNI differs, or because a load balancer or CDN has several servers holding different certificates and the script reached a different one. Connect with the exact hostname visitors use.

References and further reading

The specifications and primary documentation this article is based on.

Read next

More guides on the same subject.

Stop maintaining the script

Add your domains and CertNotify checks every certificate and registration daily. Pro adds team alerts in Slack or Teams.

Start 14-day Pro trial