ARM40-A5应用——与网络时间的同步3(发行版方式)

ARM40-A5应用——与网络时间的同步3(发行版方式)

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

  本文介绍大部分发行版使用的时间设置方式,即
  方式B(发行版方式、Windows方式,大部分发行版使用这种方式):硬件时钟为CST,系统时钟也为CST
  开机:    RTC --> CST(将硬件时钟看成是CST时间)
  更新时间:  CST --存储到–> RTC
  这种方式下,例如,date得到系统时间为19:00,则hwclock得到的时间为19: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

updatelocaltime="ntpdate 120.25.108.11"				#aliyun
#updatelocaltime="rdate -s ntp.sjtu.edu.cn"			#shanghai jiaotong edu
#updatelocaltime="rdate -s 129.6.15.28"				#time-a.nist.gov

#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
        $updatelocaltime
        tmp=$?
        #echo $tmp                    # for test
        if [ $tmp -eq 0 ]; then       # get ntp time
                hwclock -w
                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保存 hardware clock 时间(此处与方式A中的不同),然后退出。如果未能获取网络时间,则等待60s后继续获取。
  根据实际应用情况,该文件也可直接简化为 hwclock -s。

四、定期更新时间

  若出现意外故障,或人为误操作,可能导致本地系统时间与服务器时间差别较大,故每天同步一次系统时间与 hardware clock 时间。
  本例中在每天的凌晨1点,与时间服务器同步一次。
  (1)建立 /opt/user/cron_user.sh 文件,
touch /opt/user/cron_user.sh
chmod 755 /opt/user/cron_user.sh
  其内容为:

#!/bin/sh
if [ ! -d /var/spool/cron/crontabs ]; then
        mkdir -p /var/spool/cron/crontabs
fi
killall crond
crontab /etc/cron.arm40 # cron
crond

  (2)建立 /etc/cron.arm40 文件,增加一行:
0 1 * * * (rdate -s 129.6.15.28) && (hwclock -w) >/dev/null 2>&1 &
  每天的凌晨1点 rdate 从时间服务器获取网络时间,更新系统时间,并存入到 hardware clock 中。

五、启动

  (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/init.d/S49ntp 文件
  将/etc/init.d/S49ntp改名,不再运行ntpd。
cd /etc/init.d/
mv S49ntp K49ntp
  (3)/etc/profile
  在 /etc/profile 文件的最后添加:
/etc/rc.local

  (4)/etc/rc.local
touch /etc/profile.local
chmod 755 /etc/profile.local
  其内容为:

#!/bin/sh -e
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
        /opt/user/cron_user.sh                         # cron
fi
echo "profile.local done"
exit 0

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

六、测试建议

  测试阶段往往需要反复操作,如修改 /etc/cron.arm40、/opt/user/date_update.sh、/etc/profile 等,尝试各种情况。这时可能需要杀死之前的进程或服务,并重新使能。

killall crond                  # 删除服务
crontab -r                     # 删去排程
source /etc/profile            # 使 /etc/profile 的设置生效

  测试阶段还可能需要调整系统时间,观察程序是否成功校正了系统时间。
date 062513272018.00 # 设置系统时间为 2018-06-25-13:27:00

七、常见问题

  (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应用——与网络时间的同步2(内核默认方式)》
  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
  crontab 定时任务
  http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html
  crontab命令进程和作业管理
  http://man.linuxde.net/crontab
  阿里云服务器/etc/crontab文件
  荟聚计划:共商 共建 共享 Grant

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值