cronの環境変数が無意味な件について

 Linuxのcronでバグとしか思えない仕様にぶち当たったので、今回はその内容になります。

AWSのLinux 2にnginxを入れてWebサーバーにし、サーバー証明書はLet’s Encryptを使用しています。

サーバー証明書の更新は期限の30日前から実行できるので、cronを使って自動化しようとしていました。

以下はcronの設定です。

00 04 * * * certbot renew && systemctl restart nginx

毎朝4時にサーバー証明書を更新する「certbot renew」実行し、その後にnginxを再起動しています。

しかし、なぜか証明書が更新されないため、ログを出力したところ以下のエラーがありました。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/decomoco.biz.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Non-interactive renewal: random delay of 176.261823729 seconds
Could not choose appropriate plugin: The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.",)
Attempting to renew cert (decomoco.biz) from /etc/letsencrypt/renewal/decomoco.biz.conf produced an unexpected error: The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.",). Skipping.
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/decomoco.biz/fullchain.pem (failure)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/decomoco.biz/fullchain.pem (failure)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 renew failure(s), 0 parse failure(s)
Saving debug log to /var/log/letsencrypt/letsencrypt.log

どうやらcronが使用する環境変数に、nginxのインストールされているパスが含まれていないようです。
ちなみに、nginxは「/usr/sbin/」にあります。

$ which nginx
/usr/sbin/nginx

早速、cronの環境変数を調べてみると、「/usr/sbin」は含まれていました。

$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | | # * * * * * user-name command to be executed

完全にお手上げだったのでGoogle先生に聞いたところ、以下のブログを見つけました。

全くもって信じられないが、ソースコード上でパスをべた書きしており、「/usr/bin」と「/bin」のパスしか含まれないとのことです。

以下のように、コマンドを実行する前にパスを設定するよう変更した。

$ sudo crontab -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin
00 04 * * * certbot renew && systemctl restart nginx

これでやっと「certbot renew」が成功し、サーバー証明書が更新されました!!
こんなバグとしか思えないものが未だに存在するなんて、Linuxは恐ろしいです・・・

コメント

このブログの人気の投稿

ArduinoにUARTのPINを追加する

Visual StudioでAngularの開発をする

Arduino MKR WAN 1310を購入!