利用systemd实现ssl证书的自动续期

问题:因为 certbot renew 必须在证书申请30天之后才能执行成功,所以需要手动更新或者利用邮件提醒,编辑脚本等才能自动续期。我在这里想出了一种利用systemd的timer功能,实现自动续期的方法,作为参考。

解决思路: 编辑一个autocertbot.service,利用timer的定时功能,设定每个月的1-7号,且当天是星期一来运行一次该服务。

1. autocertbot.service 的编写

[Unit]
Description=Auto run certbot renew
After=network.target
 
[Service]

# 下面两行设定了再开机后60秒再启动服务
TimeoutStartSec=infinity
ExecStartPre=/bin/sleep 60

ExecStart=/usr/bin/certbot renew 

[Install]
WantedBy=multi-user.target

2. autocertbot.timer 的编写

[Unit]
Description=Run certbot renew every month's first Monday

[Timer]
OnCalendar=Mon *-*-01..07 10:00:00

[Install]
WantedBy=timers.target

这个定时器规定了 autocertbot.service 每个月的1-7号,且其中的星期一的那一天的10点钟启动。这样就可以做到每个月自动 certbot renew 一次,大于30天的那个月会运行成功。

3. 启动定时器

systemctl enable autocertbot.timer

猜你喜欢

转载自blog.csdn.net/lggirls/article/details/128135335