Automating letsencrypt certificate renewal with cron

Letsencrypt allows you to install free SSL certificates on your site. The certificates expire after 3 months and they need to be renewed to avoid certificate errors.

Depending on how you install letsencrypt, and once you configure the certificates on the site, you will want to setup automatic renewal so you don’t have to manually check and renew.

You can setup automated renewals using cron scheduling tool on linux and the following steps:

1. Sudo to root user:
sudo su –

2. Edit crontab file:
crontab -e

3. Add the following line:
00 9 * * 6 /etc/letsencrypt/.certbot-auto renew –no-random-sleep-on-renew –renew-hook ‘/sbin/service httpd restart’ > /var/log/cert_renew.log 2>&1

Let’s break down the entry to see what some of the elements mean:

00 9 * * 6
This is the schedule part, we are going to run every Saturday (day 6) at 9:00am

/etc/letsencrypt/.certbot-auto renew –no-random-sleep-on-renew
This executes the renew command and specifies that there should be no random sleep delay because of the non-interactive (no user input) mode

–renew-hook ‘/sbin/service httpd restart’
The renew hook allows running of a command upon certificate renewal, so only if the certificate or certificates were expired and had to be renewed

> /var/log/cert_renew.log 2>&1
Finally, we are redirecting the output of the command to the /var/log/cert_renew.log file.

You can also use a similar line for testing the command, the below uses the “–force-renewal” flag that will renew all certificates even if they are not expired yet:

51 21 * * * /etc/letsencrypt/.ertbot-auto renew –force-renewal –no-random-sleep-on-renew –renew-hook ‘/sbin/service httpd restart’ > /var/log/cert_renew.log 2>&1

Styling nested elements with LESS

LESS is a great tool for removing the tediousness out of CSS styling by allowing nested selectors and use of variables.
But there is much more to the language allowing for more complex logic to be embedded. One of the great features of the language is the ability to loop and create styles recursively.

A good practical use for this functionality is to style a comments section:

See the Pen Styling nested elements with LESS by Marcin Jackowiak (@marcinjackowiak) on CodePen.

Below is the basic LESS code will generate CSS that varies the div background color depending on how deep the element is in the DOM hierarchy.
It is a prime example of something that could be a nightmare to maintain directly in the CSS stylesheet.

#namespace {
  .test(@i,@x) {
    div {
      background-color: lighten(red, @i*(50/@x));
    }
  }

  .recursive(@x; @i:0) when (@i <= @x) {
      .test(@i,@x);
      div {
        .recursive(@x; (@i+1));
      }
  }
}

div {
  width: 200px;
  height: 200px;
  padding: 20px;
  border: 1px solid #888;
}

& { #namespace > .recursive(5); }

What it generates is a nice gradient of red that will lighten the deeper you go.
The following formula will lighten the color by 50% with each level. To create a smoother transition simply lower the
number.

      background-color: lighten(red, @i*(50/@x));

The argument in the .recursive mixin allows you to specify the depth to which the styles will be generated.

& { #namespace > .recursive(5); }

The resulting CSS is as follows.

div {
  width: 200px;
  height: 200px;
  padding: 20px;
  border: 1px solid #888;
}
div {
  background-color: #ff0000;
}
div div {
  background-color: #ff3333;
}
div div div {
  background-color: #ff6666;
}
div div div div {
  background-color: #ff9999;
}
div div div div div {
  background-color: #ffcccc;
}
div div div div div div {
  background-color: #ffffff;
}