Moxa DA-682A-C7-W7E [11/112] Using a shell script for automatic updates

Moxa DA-682A-C7-W7E [11/112] Using a shell script for automatic updates
DA-682A Linux Softrware Software Configuration
2-5
moxa@MOXA:~# date ; sudo hwclock
Wed Dec 16 16:36:12 CST 2009
Wed 16 Dec 2009 03:38:13 AM CST -0.016751 seconds
moxa@MOXA:~#
moxa@MOXA:~# sudo ntpdate time.stdtime.gov.tw
16 Dec 03:49:48 ntpdate[2510]: step time server 220.130.158.52 offset
155905087.9
84256 sec
moxa@MOXA:~#
moxa@MOXA:~# sudo hwclock -w
moxa@MOXA:~# date ; sudo hwclock
Wed Dec 16 03:51:07 CST 2009
Wed 16 Dec 2009 03:51:07 AM CST -0.016771 seconds
ATTENTION
Before using the NTP client utility, check your IP address and network settings (gateway and DNS) to make
sure an Internet connection is available.
You may visit the Network Time Protocol project’s home page
http://www.ntp.org
(Oct, 2013) for more information about NTP.
Using a Shell Script for Automatic Updates
As the RTC gets older, it may start to run slow and fail to accurately track time. This section provides one
example of how a shell script may be used to repeatedly synchronize the RTC to the system clock by using the
Liinux initialization table (inittab). Because the system clock will be automatically synched using NTP, the two
clocks will reliably keep time. Other methods are also available, for instance using cron (shown below, in the
section Cron for Executing Scheduled Commands) or using the at command. Below, we show you how to
write a simple shell script for keeping the two clocks synchronized, and show you how to set the system to
continuously run the script in the background, across re-boots.
Sample shell script for scheduled clock synchronizations
You may save this shell script using any file name, but it should be saved in the /etc/init.d directory. For
example,
/etc/init.d/fixtime.sh.
#!/bin/sh
ntpdate time.stdtime.gov.tw
# You can use the time server’s ip address or domain
# name directly. If you use domain name, you must
# enable the domain client on the system by updating
# /etc/resolv.conf file.
hwclock –w
sleep 100
# Updates every 100 seconds. The min. time is 100 seconds.
# Change 100 to a larger number to update the RTC less often.
How to run a shell script automatically across re-boots
Copy the example above shell script fixtime.sh to the directory /etc/init.d, and then set its access
permissions to 755.
moxa@MOXA:~# chmod 755 fixtime.sh
Next, use open the initialization table (inittab) for editing in your preferred editor (we use VI as an example):
moxa@MOXA:~# vi /etc/inittab
Add the following line to the bottom of the file:
ntp : 2345 : respawn : /etc/init.d/fixtime.sh

Содержание

Похожие устройства