移植NTP时间同步工具到arm linux平台创建定时任务

下载源码

wget -c http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p14.tar.gz

解压并编译

在这里插入图片描述

./configure --prefix=$PWD/install --exec-prefix=$PWD/install --host=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc --with-yielding-select=yes
make
make install

一个脚本进行编译

#!/bin/sh
HOST=arm-none-linux-gnueabi
BUILD_HOST=/home/aron566/opt/arm-2014.05/bin/arm-none-linux-gnueabi-
ARM_GCC=${BUILD_HOST}gcc
ARM_CPP=${BUILD_HOST}g++
BASE=`pwd`
OUTPUT_PATH=${BASE}/install

make_dirs () {
    
    
    #为了方便管理,创建有关的目录
    cd ${BASE} && mkdir compressed install source -p
}

download_package () {
    
    
    cd ${BASE}/compressed
    #下载包
	wget -c http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p14.tar.gz
}

tar_package () {
    
    
    cd ${BASE}/compressed
    ls * > /tmp/list.txt
    for TAR in `cat /tmp/list.txt`
    do
        tar -xf $TAR -C ../source
    done
    rm -rf /tmp/list.txt
}

make_ntp () {
    
    
  cd ${BASE}/source/ntp-4.2.8p14
	./configure --prefix=$OUTPUT_PATH/ntp-4.2.8p14 --exec-prefix=$OUTPUT_PATH/ntp-4.2.8p14 --host=$HOST CC=$ARM_GCC --with-yielding-select=yes
	make
	make install
}

make_dirs
download_package
tar_package
make_ntp
exit $?

上传文件至开发板

install 目录下 bin目录下执行件

复制到开发板的/usr/bin目录下

运行

同步时间命令

ntpdate time.buptnet.edu.cn

使用date查看时间

date命令查看之后显示的是UTC时间(世界标准时间),比北京时间(CST=UTC+8)相差8个小时,所以需要设置时区

设置时区

ubuntu系统目录/usr/share/zoneinfo/Asia中的文件Shanghai拷贝到开发板目录/etc中并且改名为localtime之后,用命令reboot重启即可

显示UTC时间可以使用命令

date -u

从网络上获取到的系统时间写入硬件时钟中

hwclock -w

创建开机启动项

S92crontab加入到/etc/init.d目录中,添加可执行权限

#!/bin/sh
#无需修改
DIRECTORY_NAME=/etc/crontabs
#无需修改
CONFIG_NAME=$DIRECTORY_NAME/root
#运行时配置,无需修改
RUN_DIRECTORY_NAME=/var/spool/cron/crontabs
RUN_CONFIG_NAME=$RUN_DIRECTORY_NAME/root

#定时任务
TASK_1="*/1 */1 */1 */1 */1 ntpdate time.buptnet.edu.cn"
TASK_2="*/1 */1 */1 */1 */1 hwclock -w"
start() {
    
    
	#是否存在定时任务配置目录
	if [ ! -d "${DIRECTORY_NAME}" ];then
		mkdir $DIRECTORY_NAME -p
		touch $CONFIG_NAME
		chmod 777 $CONFIG_NAME
		echo $TASK_1>>$CONFIG_NAME
		echo $TASK_2>>$CONFIG_NAME
	fi
	mkdir -p $RUN_DIRECTORY_NAME
	cp -p $CONFIG_NAME $RUN_CONFIG_NAME
	chmod 777 $RUN_CONFIG_NAME
	crond
	[ $? = 0 ] && echo "START Crontabs OK" || echo "START Crontabs FAIL"
}
stop() {
    
    
	echo "STOP Crontabs"
}
restart() {
    
    
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

注意在windows上编写的文件可能需要执行以下命令

dos2unix S92crontab #消除断元符

ntp服务器

1、保证做ntp服务器的开发板和ntp客户端的系统的网络连接正常

2、在要做服务器的开发板上编辑文件/etc/ntp.conf
执行命令:

vi  /etc/ntp.conf 
#restrict default nomodify notrap noquery 
restrict 127.0.0.1 
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 
server  127.127.1.0        # local clock 
fudge   127.127.1.0  stratum 5 
driftfile /var/lib/ntp/drift 
broadcastdelay   0.008 
keys                     /etc/ntp/keys

3、然后开启ntp服务器

ntpd  -c /etc/ntp.conf

4、ntp服务器开启后五到十几分钟才能在客户端系统中执行以下命令,否则时间同步会失败

ntpdate  192.168.1.48  #192.168.1.48修改成你的服务器的IP地址

猜你喜欢

转载自blog.csdn.net/weixin_42892101/article/details/106877000