dateコマンドで時間を見ると案の定ずれている。
1 2 |
[root@raspi ~]# date Thu Jun 20 16:51:45 EDT 2013 |
とりあえずタイムゾーンを変更
1 2 3 |
[root@raspi ~]# timedatectl set-timezone Asia/Tokyo [root@raspi ~]# date Fri Jun 21 05:55:43 JST 2013 |
JSTになったけど時間はズレている。
一時的対応としてntpdateで時間を合わせる
1 2 3 4 |
[root@raspi ~]# ntpdate ntp.jst.mfeed.ad.jp 21 Apr 21:11:10 ntpdate[1197]: step time server 210.173.160.57 offset 26320336.860236 sec [root@raspi ~]# date Mon Apr 21 21:11:15 JST 2014 |
これで時間があったが、これでは毎回起動の度に時間がずれてしまう。
1 2 3 |
[root@raspi ~]# clock -w clock: Cannot access the Hardware Clock via any known method. clock: Use the --debug option to see the details of our search for an access method. |
なんとなく知っていたが、raspberryPiはハードウェア時計を持っていないので、時間を記憶できない。
広告
起動時&定期的に時刻を合わせる
起動時に1回だけでもいいかもしれないのですが、WEBサーバーとして長時間動作させる予定なので、定期的に時間を合わせるようにします。
時刻同期スクリプトの作成
時刻を同期するスクリプトを作成します。スクリプトといってもntpdateコマンド1行です。
1 |
[root@raspi bin]# vi /usr/bin/checkdate.sh |
下記を記述して保存
1 2 |
#!/bin/sh /usr/sbin/ntpdate ntp.jst.mfeed.ad.jp |
実行できるよう属性を変更します。
1 |
[root@raspi bin]# chmod 700 /usr/bin/checkdate.sh |
cronで定期的に実行
定期的にプログラムを実行したいので、cronに登録します。
1 |
crontab -e |
毎時1分に実行して欲しいので↓こんな感じに記述して保存
1 |
01 * * * * /usr/bin/checkdate.sh |
で、実際に動くか待ったのですが、動かない。あれ、crond動いてないとかないよね?
調べてみる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
[root@raspi ~]# systemctl UNIT LOAD ACTIVE SUB JOB DESCRIPTION boot.automount loaded active running boot.automount proc-sys...misc.automount loaded active waiting Arbitrary Executable File sys-devi...ttyAMA0.device loaded active plugged /sys/devices/dev:f1/tty/tt sys-devi...et-eth0.device loaded active plugged ec00 sys-devi...da-sda1.device loaded active plugged HDPC-U sys-devi...da-sda2.device loaded active plugged HDPC-U sys-devi...ock-sda.device loaded active plugged HDPC-U sys-devi...d-card0.device loaded active plugged /sys/devices/platform/bcm2 sys-devi...cblk0p1.device loaded active plugged /sys/devices/platform/mmc_ sys-devi...cblk0p2.device loaded active plugged /sys/devices/platform/mmc_ sys-devi...mmcblk0.device loaded active plugged /sys/devices/platform/mmc_ sys-devi...y-ttyS1.device loaded active plugged /sys/devices/platform/seri sys-devi...y-ttyS2.device loaded active plugged /sys/devices/platform/seri sys-devi...y-ttyS3.device loaded active plugged /sys/devices/platform/seri sys-devi...y-ttyS0.device loaded active plugged /sys/devices/platform/seri sys-devi...ock-md0.device loaded active plugged /sys/devices/virtual/block sys-subs...es-eth0.device loaded active plugged ec00 -.mount loaded active mounted / boot.mount loaded active mounted /boot tmp.mount loaded active mounted Temporary Directory systemd-...d-console.path loaded active waiting Dispatch Password Requests systemd-...word-wall.path loaded active waiting Forward Password Requests accounts-daemon.service loaded active running Accounts Service bluetooth.service loaded active running Bluetooth service console-...daemon.service loaded active running Console Manager console-...-start.service loaded active exited Console System Startup Log dbus.service loaded active running D-Bus System Message Bus firewalld.service loaded active running firewalld - dynamic firewa lightdm.service loaded active running Light Display Manager NetworkManager.service loaded active running Network Manager polkit.service loaded active running Authorization Manager rsyslog.service loaded active running System Logging Service sendmail.service loaded active running Sendmail Mail Transport Ag sm-client.service loaded active running Sendmail Mail Transport Cl smartd.service loaded active running Self Monitoring and Report sshd.service loaded active running OpenSSH server daemon systemd-journald.service loaded active running Journal Service systemd-logind.service loaded active running Login Service systemd-...unt-fs.service loaded active exited Remount Root and Kernel Fi systemd-sysctl.service loaded active exited Apply Kernel Variables systemd-...-setup.service loaded active exited Recreate Volatile Files an systemd-...rigger.service loaded active exited udev Coldplug all Devices systemd-udevd.service loaded active running udev Kernel Device Manager systemd-...ssions.service loaded active exited Permit User Sessions systemd-...-setup.service loaded active exited Setup Virtual Console upower.service loaded active running Daemon for power managemen dbus.socket loaded active running D-Bus System Message Bus S dm-event.socket loaded active listening Device-mapper event daemon syslog.socket loaded active running Syslog Socket systemd-initctl.socket loaded active listening /dev/initctl Compatibility systemd-journald.socket loaded active running Journal Socket systemd-shutdownd.socket loaded active listening Delayed Shutdown Socket systemd-...control.socket loaded active listening udev Control Socket systemd-...-kernel.socket loaded active running udev Kernel Socket dev-sda1.swap loaded active active /dev/sda1 basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts graphical.target loaded active active Graphical Interface local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User network.target loaded active active Network remote-fs.target loaded active active Remote File Systems sockets.target loaded active active Sockets sound.target loaded active active Sound Card swap.target loaded active active Swap sysinit.target loaded active active System Initialization syslog.target loaded active active Syslog systemd-...es-clean.timer loaded active waiting Daily Cleanup of Temporary LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. JOB = Pending job for the unit. 70 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. |
動いてません。
動かします。ついでに自動起動設定も行います。
1 2 3 |
[root@raspi ~]# systemctl start crond.service [root@raspi ~]# systemctl enable crond.service |
これで大丈夫でした。毎時1分に時間がNTPサーバーと同期します。
起動時に実行するよう設定
起動時にプログラムが実行されるように、rc.localを作成します。
1 |
[root@raspi ~]# vi /etc/rc.d/rc.local |
実行するコマンドを書きます。
1 2 |
#!/bin/sh /usr/bin/checkdate.sh |
同じくntpdateコマンドを記述しても良かったのですが、なんとなく一緒にしておきたかったので、上で作成したシェルを実行します。
再起動して確認
起動時に時刻が設定されるようになりました
追記:時刻同期のタイミングを毎時から8時間に1回に変更することに。cronはこんな↓感じに
1 2 |
</pre> 01 */8 * * * /usr/bin/checkdate.sh |