受欢迎的博客标签

How to Configuring Automatic LetsEncrypt SSL Certificate Renewal

Published

 

Ubuntu 18.04.6 LTS on aliyun vps

# lsb_release -a
LSB Version:	core-9.20170808ubuntu1-noarch:security-9.20170808ubuntu1-noarch
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

 

 

certbot-renew.sh

#!/bin/bash

echo "==================Certbot Renew====================="
echo "==================`date`============================"
LOG_FILE="/var/log/letsencrypt/renewal.log"
echo "$(date) - 开始证书续期" >> $LOG_FILE

sudo certbot certificates
service nginx stop
certbot renew
sudo certbot certificates
service nginx restart

echo "==================End==============================="

 

# certbot --version
certbot 2.8.0

 

使用 crontab -l命令列出前的所有定时任务

 

A Ubuntu system has the following cron tab files and directories:

系统级别预定义用于存放定时任务脚本的目录 :
/etc/cron.hourly/:每小时执行的脚本
/etc/cron.daily/:每天执行的脚本
/etc/cron.weekly/:每周执行的脚本
/etc/cron.monthly/:每月执行的脚本

@iZnh03:/etc# ls  cron*
cron.d:
cron.daily:
cron.hourly:
cron.monthly:
cron.weekly:

 

vi /etc/cron.d/certbot

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

 

crontab

 

0 3 1 * * /ktt/crons/certbot-renew.sh &>> /ktt/crons/certbot-renew.log

&>>
& 指将标准输出和标准错误输出都重定向到certbot-renew.log文件;
> 指覆盖写入到文件;
>> 指追加写入到文件。

sudo crontab -e

0 3 1 * * /ktt/crons/certbot-renew.sh &>> /ktt/crons/certbot-renew.log

0 3 1 * * 指每个月1号3点0分执行任务

cron job cerbot Let‘s Encrypt证书自动续期

Useful links

crontab的语法规则格式(每分钟、每小时、每天、每周、每月、每年定时执行 规则)

https://peakxin.blog.csdn.net/article/details/83178876

 

sudo crontab -e

特性
用户级定时任务 (crontab -e)
系统级定时任务 (/etc/crontab)
​配置文件路径​
/var/spool/cron/crontabs/<用户名>
7
/etc/crontab
​编辑方式​
crontab -e
直接使用 root 权限编辑文件(如 sudo vim /etc/crontab
​任务归属​
属于执行命令的相应用户
需在配置中指定执行用户(如 root
​适用场景​
用户自定义的个性化任务(如备份脚本、数据同步)
系统维护任务(如日志轮转、系统清理)