ARM40-A5应用——与网络时间的同步2(内核默认方式)

本文介绍ARM40-A5设备通过网络进行时间同步的方法,包括设置时区、上电时与网络时间同步、定期更新时间及常见问题解决。采用内核默认方式,系统时钟为CST,硬件时钟为UTC。

ARM40-A5应用——与网络时间的同步2(内核默认方式)

2018.10.7
版权声明:本文为博主原创文章,允许转载。

  本文介绍Linux内核默认的时间设置方式,即
  方式A(内核方式,Linux内核默认的方式): 硬件时钟为UTC(世界标准时间,协调世界时),系统时钟为CST
  开机:    RTC --> UTC(RTC中存放UTC时间)–> 读取时区 --> 系统时钟为CST
  更新时间:  系统时钟为CST --> 将UTC时间存入RTC
  这种方式下,例如,date得到系统时间为19:00,则hwclock得到的时间为11:00.

一、要有网。

  ARM40-A5与网络时间同步,当然要有网。如果是4G或GPRS联网方式可参考《ARM40-A5应用——GPRS模块ppp拨号上网》《ARM40-A5应用——4G模块EC20-CE拨号上网》。

二、设置时区

  /etc/localtime 文件中设置了时区为CST-8,该文件拷贝自ubuntu中的 /usr/share/zoneinfo/Asia/Shanghai 文件。
  使用 strings 命令可以看到CST-8. 如果需要设置其它时区,可从ubuntu中将相应的文件拷贝为/etc/localtime.
root@ARM40:/# strings /etc/localtime

三、上电时与网络时间同步

  刚上电时,可能本地系统时间与服务器时间差别较大,故同步一次。
  由 /opt/user/date_update.sh 文件完成上电时的同步功能,每60s同步一次,直到成功后退出。
  另建议在设备安装启用时,检查系统时间是否同步成功,一般在5分钟内,就会同步成功。
  建立/opt/user/date_update.sh文件,
touch /opt/user/date_update.sh
chmod 755 /opt/user/date_update.sh
  其内容为:

#!/bin/sh
hwclock -s
ntpServer="129.6.15.28"
#echo "nameserver 223.5.5.5" > /etc/resolv.conf    # aliyun DNS1
#echo "nameserver 223.6.6.6" >> /etc/resolv.conf   # aliyun DNS2
while true                            # when startup,update date
do
        sleep 60
        rdate -s $ntpServer
        tmp=$?
        #echo $tmp                    # for test
        if [ $tmp -eq 0 ]; then       # get ntp time
                hwclock -w --utc
                echo "Date is updated" > /dev/console
                exit 0
        else                          # can't get ntp time
                procnum=`ps -ef|grep "rdate"|grep -v grep|wc -l`
                if [ $procnum -ne 0 ]; then
                        killall rdate
                fi
        fi
done

  简单介绍 /opt/user/date_update.sh 文件的功能:
  系统启动后,首先将硬件时间设置为系统时间,即hwclock -s,然后 rdate -s $ntpServer 获取服务器的网络时间,如果成功,则 hwclock -w --utc保存 hardware clock 时间(此处与方式B中的不同),然后退出。如果未能获取网络时间,则等待60s后继续获取。
  根据实际应用情况,该文件也可直接简化为 hwclock -s。

四、定期更新时间

  使用ntpd更新时间,ARM40-A5在电时,由/etc/init.d/S49ntp启动ntdp服务校准系统时间,该服务每64s校时一次。
  命令 ps 可看到ntpd服务 /usr/sbin/ntpd -g ,命令 ntpq -p 可以看到校时情况。
  ntpd在本地系统时间与服务器时间差别较大时(1000s),则不会继续更新系统时间了。
  约11分钟,ntpd会将硬件时间与系统时间同步一样。

五、启动

  (1)/etc/inittab 文件
  修改 /etc/inittab中的
console::respawn:/sbin/getty -L console 0 vt100 # GENERIC_SERIAL
#console::respawn:-/bin/sh
  为:
#console::respawn:/sbin/getty -L console 0 vt100 # GENERIC_SERIAL
console::respawn:-/bin/sh
  (2)/etc/profile
  在 /etc/profile 文件的最后添加:
/etc/rc.local
  (3)/etc/profile.local
touch /etc/profile.local
chmod 755 /etc/profile.local
  其内容为:

reset
logintty=$(tty|grep -c "console")
if [ $logintty -eq 1 ]; then                           # ssh,telnet can't get into this line
        /opt/user/date_update.sh >/dev/null 2>&1 &     # when startup,update date
fi
echo "profile.local done"

  修改完毕后 source /etc/profile 使 /etc/profile 的设置生效;或者 reboot 重启ARM40-A5,观察程序运行的情况。

六、常见问题

  (1)时间服务器
  在NTP官方网站可找到提供同步服务的NTP SERVER,当然也可以自己搭建NTP服务器。
  NTP官网地址: http://www.pool.ntp.org/en/
  (2)rdate时间服务器
  更多的rdate时间服务器见 https://tf.nist.gov/tf-cgi/servers.cgi
  (3)rdate -s $ntpServer 同步网络时间可能出现的情况:

# rdate -s 129.6.15.28
rdate: current time matches remote time              # 同步成功,返回值为0
# rdate -s 129.6.15.28
rdate: timeout connecting to time server             # 超时,返回值为1
# rdate -s 129.6.15.28
rdate: can't connect to remote host (129.6.15.28): Network is unreachable    # 无法联网,返回值为1
# rdate -s time.nist.gov
rdate: bad address 'time.nist.gov'                   # DNS error,返回值为1
# rdate -s 129.6.15.28
rdate: 129.6.15.28: short read                       # 其它错误,返回值为1

参考文章:

  date命令 http://man.linuxde.net/date
  ntpdate命令 http://man.linuxde.net/ntpdate
  《ARM40-A5应用——GPRS模块ppp拨号上网》
  《ARM40-A5应用——4G模块EC20-CE拨号上网》
  《ARM40-A5应用——与网络时间的同步1(概述)》
  《ARM40-A5应用——与网络时间的同步3(发行版方式)》
  NIST Internet Time Servers https://tf.nist.gov/tf-cgi/servers.cgi
  在NTP官方网站官网地址: http://www.pool.ntp.org/en/
  《鳥哥的 Linux 私房菜》第十五章、時間伺服器: NTP 伺服器
  http://linux.vbird.org/linux_server/0440ntp.php
  荟聚计划:共商 共建 共享 Grant

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值