场景:使用 Certbot 和 Cron 实现 SSL 证书的自动续期。
代码示例:
#!/bin/bash
# renew-certs.sh
# 续期证书
certbot renew --quiet --post-hook "systemctl reload nginx"
# 检查续期结果
if [ $? -eq 0 ]; then
echo "Certificates renewed successfully."
else
echo "Certificate renewal failed!"
exit 1
fi
Cron 配置:
# 每天凌晨 2 点检查证书续期
0 2 * * * /path/to/renew-certs.sh >> /var/log/certbot-renew.log 2>&1
文字说明:
-
使用 Certbot 自动管理 SSL 证书,避免证书过期导致的服务中断。
-
通过 Cron 定时任务实现无人值守的证书续期。
-
适用于 HTTPS 服务的运维管理。