ラズパイの時刻を合わせて自動起動する

Raspberry Pi (RasPi) を電源に接続して常時稼働させ続けていると、時計の時刻がズレていたり、USB接続している外部デバイスを読み込めなくなっていることがあります。

そんな時は標準時刻サーバの公開 NTP(Network Time Protocol)に時刻を問合せて、定期的に RasPi の再起動を実行すれば問題が解決することがあります。

日本標準時 (JST) に関しては NICT(ニクト)こと国立研究開発法人情報通信研究機構という国立研究機関が配信を行っていますので、こちらに問合せを行えば大丈夫です。

香港時間なら香港天文台、中央ヨーロッパ時間なら PTB という物理工学研究所といった具合に標準時刻サーバは複数ありますので必要に応じて書き換えてください。

まずは実行環境から

$ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Debian 派生の Raspbian GNU/Linux 10 では時刻同期にシステムサービス systemd-timesyncd を使用します。これを用いるためにはエディタを使って /etc/systemd/timesyncd.conf を開き、

#NTP=

と書かれている行のコメントアウトを削除して ntp.nict.jp のアドレスを書き込みます。

$ vim /etc/systemd/timesyncd.conf 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=ntp.nict.jp 
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

FallbackNTP は NTP が定義されている場合には無視されますので気にしなくて結構です。

設定ファイルを書き換えましたらシステムサービスの NTP 機能を有効化して、状態を表示します。

$ sudo timedatectl set-ntp true
$ sudo timedatectl status
               Local time: Sat 2020-09-05 23:24:53 CDT
           Universal time: Sun 2020-09-06 04:24:53 UTC
                 RTC time: n/a
                Time zone: America/Chicago (CDT, -0500)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

ステータス表示 NTP service: active になっていれば、NTP 機能が稼働しています。




ただ、私の環境ではタイムゾーンがおかしなことになっているので、こちらも変更しておきます。

$ sudo cp /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

準備が整いましたら設定を反映させるためにシステムサービスを再起動して、ステータスを表示します。

$ sudo systemctl restart systemd-timesyncd.service
$ sudo systemctl status systemd-timesyncd.service
\u25cf systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vend
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           \u2514\u2500disable-with-time-daemon.conf
   Active: active (running) since Sun 2020-09-06 12:45:38 HKT; 11s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 22048 (systemd-timesyn)
   Status: "Synchronized to time server for the first time [2001:df0:232:eea0::fff4]:123 (ntp.nict.jp)."
   Memory: 856.0K
   CGroup: /system.slice/systemd-timesyncd.service
           \u2514\u250022048 /lib/systemd/systemd-timesyncd

以下のようにNICT サーバに接続できていることが表示されれば成功です。

“Synchronized to time server for the first time [2001:df0:232:eea0::fff4]:123 (ntp.nict.jp).”

もし以下の例のように NICT 以外のサーバに接続している場合には、設定内容に誤りがあるか、設定が反映されていない場合がありますので見直しを行ってください。

Status: “Synchronized to time server for the first time 194.0.5.123:123 (0.debian.pool.ntp.org).”

また、ネットワークに接続できない環境で Ras Pi をお使いの場合には、以下のコマンドで手動で時刻合わせを行うことも可能です。

$ date +%T -s "13:00:00"

時刻合わせが完了しましたら、あとは再起動しても問題ない時間に reboot するように cron を設定しておけば、毎日、決まった時間にシステムを停止して再起動を行うようにできます。

$ sudo crontab -e

# m h  dom mon dow   command
0 0 * * * /sbin/reboot

こうしておけば、より長時間安定して稼働し続けることが期待できるはずです。

Leave a Reply

Your email address will not be published. Required fields are marked *