Skip to content

Instantly share code, notes, and snippets.

@ahmedelgabri
Last active January 20, 2022 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedelgabri/cba569863cfed73eeee2614d28a02004 to your computer and use it in GitHub Desktop.
Save ahmedelgabri/cba569863cfed73eeee2614d28a02004 to your computer and use it in GitHub Desktop.
Cron job for automatic renewal of Let’s Encrypt certs, it should run twice a day at a random minute.

This is a cron job entry to that will auto renew your Let's Encrypt certificate automatically by running 2 times a day evey 12 hours on a random minute.

You can add a cron job entry by running this command

$ crontab -e

Then past the following snippit in that file, Note that lines 1-3 might not be required at all. Make sure your check the link to the thread regarding this.

I'm not a DevOps/Server guy, so any improvements are highly appreciated.

# This is required for vixie-cron (man cron)
# Check http://superuser.com/questions/264528/problem-with-random-in-crontab/264541#264541
SHELL=/bin/bash
# Auto renew Let’s Encrypt certs, two times a day at a random minute.
# https://certbot.eff.org/
# http://crontab.guru/#0_*/12_*_*_*
# http://stackoverflow.com/a/16289693/213124
# http://blog.buberel.org/2010/07/howto-random-sleep-duration-in-bash.html
0 */12 * * * sleep $[($RANDOM % 60) + 1]m; /root/certbot/certbot-auto renew --quiet --no-self-upgrade
@tareksamni
Copy link

You can also replace SHELL=/bin/bash with #!/bin/bash

@ls-michielrensen
Copy link

Nice touch on the random minute, but why?
I use this on my VPS, works like a charm: https://github.com/hlandau/acme

@ahmedelgabri
Copy link
Author

@michielrensen will check it! (I hate that gists doesn't have any kind of notifications)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment