導入したSSL証明書( Let’s Encrypt )の更新をcronで自動化する
参考URL
cronの稼働状況を確認する
cronieパッケージがインストールされているか確認
# yum list cronie
(略)
Installed Packages
cronie.x86_64                                     1.4.11-17.el7                                      @base
- ある(centOS7でデフォルトらしい)
 
cronが稼働しているか確認
# systemctl status crond
#  crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-11-09 14:29:50 JST; 1 weeks 2 days ago
(略)
- active (running) になっているのでOK
 
サーバー起動時に起動するようになっているか確認
# systemctl list-unit-files | grep cron
crond.service                                 enabled
- enabled になっているのでOK
 
cronの設定ファイルに証明書更新コマンドを追記する
cronで実行したいコマンドのフルパスを調べる
# which certbot
/usr/bin/certbot
- 証明書更新のコマンドをフルパスから書くと以下のようになる
 
/usr/bin/certbot renew
- 有効期限の残りが30日未満の場合にのみ更新される
 - 即時更新の場合は –force-renew オプションを加える
 
cronの設定ファイルを編集する
# vi /etc/crontab
      1 SHELL=/bin/bash
      2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
      3 MAILTO=root
      4 
      5 # For details see man 4 crontabs
      6 
      7 # Example of job definition:
      8 # .---------------- minute (0 - 59)
      9 # |  .------------- hour (0 - 23)
     10 # |  |  .---------- day of month (1 - 31)
     11 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
     12 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
     13 # |  |  |  |  |
     14 # *  *  *  *  * user-name  command to be executed
- 書式に則って証明書更新コマンドを記述
- 毎日3:00にroot権限で実行する場合
 
 
| 分 | 時 | 日 | 月 | 曜日 | ユーザ名 | 実行コマンド | 
|---|---|---|---|---|---|---|
| 0 | 3 | * | * | * | root | /usr/bin/certbot renew | 
0  3  *  *  * root /usr/bin/certbot renew
- 2017-11-18現在、有効期限が「2018年2月16日」となっているので、1/20くらいに更新されたかどうか確認してみる。
 
設定後のcron再起動は必要か?
cron の場合、設定ファイルを保存した時点で、cron が自動的に変更を検知するため、cron デーモンの再起動は必要ありません。
第30回 「cron のお勉強」