0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Linux 2023 systemd timer ― 設定不要ですぐ使える証明書自動更新

Posted at

systemd timerとは

systemd timerは、Linuxのsystemdに組み込まれている定期実行の仕組みです。従来のCronに代わる、より高機能で信頼性の高いスケジューラーとして使われています。

Amazon Linux 2023では、Let's Encryptの証明書自動更新にsystemd timerがデフォルトで採用されています。

Cronとの違い

項目 Cron systemd timer
設定方法 crontab -e systemctl enable/start
ログ管理 /var/log/cron systemd journal(journalctl
PATH問題 発生しやすい※ 発生しにくい
依存関係管理 なし あり
停止中のタスク実行 なし Persistent機能あり
ランダム遅延 手動設定必要 ビルトイン
設定の複雑さ シンプル やや複雑

※ Cronでは環境変数PATHが最小限しか設定されないため、/usr/sbinなどが含まれずコマンドが見つからない問題が起きやすい

基本的な使い方

タイマーの有効化と起動

# タイマーを有効化(システム再起動後も自動起動)
sudo systemctl enable certbot-renew.timer

# タイマーを起動
sudo systemctl start certbot-renew.timer

タイマーの状態確認

# タイマーの状態を確認
sudo systemctl status certbot-renew.timer

出力例:

● certbot-renew.timer - This is the timer to set the schedule for automated renewals
     Loaded: loaded (/usr/lib/systemd/system/certbot-renew.timer; enabled; preset: disabled)
     Active: active (waiting) since Thu 2025-10-09 13:01:49 UTC; 6min ago
    Trigger: Fri 2025-10-10 02:51:50 UTC; 13h left
   Triggers: ● certbot-renew.service
  • Loaded: 設定ファイルが読み込まれている
  • enabled: システム再起動後も自動で起動される
  • Active: active (waiting): タイマーが稼働中で次回実行を待機している
  • Trigger: 次回実行予定時刻

実行スケジュールの確認

# 全タイマーの実行予定を確認
sudo systemctl list-timers | grep certbot

出力例:

Fri 2025-10-10 02:51:50 UTC 13h left     -     certbot-renew.timer     certbot-renew.service
  • NEXT: 次回実行予定時刻
  • LEFT: 実行まであと何時間・何分か

設定ファイルの確認

systemctl cat certbot-renew.timer

出力例:

# /usr/lib/systemd/system/certbot-renew.timer
[Unit]
Description=This is the timer to set the schedule for automated renewals

[Timer]
OnCalendar=*-*-* 00/12:00:00
RandomizedDelaySec=12hours
Persistent=true

[Install]
WantedBy=timers.target

設定の意味

[Timer]セクション

  • OnCalendar=--* 00/12:00:00

    • 実行スケジュール:12時間ごと(0時と12時)
    • 書式:年-月-日 時/間隔:分:秒
  • RandomizedDelaySec=12hours

    • ランダムな遅延時間:0〜12時間
    • サーバーの負荷分散のため、実行時刻をずらす
  • Persistent=true

    • システム停止中に実行予定だったタスクを、起動後に実行する

[Install]セクション

  • WantedBy=timers.target
    • システム起動時にこのタイマーを自動で開始する

systemd timerの利点

1. ログ管理が統合されている

Cronはログが分散しがちですが、systemd timerはjournalctlで一元管理できます。

# 実行ログの確認
sudo journalctl -u certbot-renew.service

# 最新10件を表示
sudo journalctl -u certbot-renew.service -n 10

# リアルタイムで監視
sudo journalctl -u certbot-renew.service -f

2. PATH問題が起きにくい

Cronでは環境変数が最小限しか設定されないため、以下のような問題が起きることがあります:

nginx: command not found

systemd timerではサービスファイルで環境変数を適切に設定できるため、このような問題が起きにくくなっています。

3. Persistent機能

システムが停止している間に実行予定だったタスクを、起動後に実行してくれます。

  • サーバーメンテナンスで停止中
  • バックアップタスクの実行予定時刻だった
  • 起動後、自動的にバックアップが実行される ✅

4. 依存関係管理

「サービスAが完了してからサービスBを実行」といった依存関係を定義できます。

5. ランダム遅延がビルトイン

複数のサーバーが同時に外部サービス(Let's Encryptなど)にアクセスしないよう、実行時刻をずらす機能が標準で備わっています。

実際の使用例:Let's Encrypt証明書の自動更新

Amazon Linux 2023にcertbotをインストールすると、自動的に以下が設定されます:

  1. /usr/lib/systemd/system/certbot-renew.timer - タイマー設定
  2. /usr/lib/systemd/system/certbot-renew.service - 実行するサービス

あとは有効化するだけ:

sudo systemctl enable certbot-renew.timer
sudo systemctl start certbot-renew.timer

これで、12時間ごとに証明書の更新チェックが自動で実行され、有効期限が30日以内になったら自動で更新されます。

まとめ

  • systemd timerは、Cronに代わる新しい定期実行の仕組み
  • Amazon Linux 2023などの新しいディストリビューションで標準採用
  • ログ管理、依存関係、Persistent機能など、Cronより高機能
  • certbotなどの重要なタスクの自動実行に適している
  • 設定はsystemctl enablesystemctl startだけでOK

cronよりモダンで信頼性が高い仕組みなので、新しいサーバー構築時は積極的に使っていきましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?