DA-681 Linux
Software Configuration
2-6
Updating the Time Automatically
This section describes how to use a shell script to update the time automatically.
Example shell script for updating the system time periodically
#!/bin/sh
ntpdate time.nist.gov
# 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 RTC less often.
Save the shell script using any file name. For example,
fixtime
.
How to run the shell script automatically when the kernel boots up
Because the root file system is mounted in Read-only mode, we need to re-mount it using writable permission.
# mount -o remount,rw /dev/hda1 /
Copy the example shell script
fixtime
to directory
/etc/init.d
, and then use
chmod 755 fixtime
to change
the shell script mode.
# chmod 755 fixtime
Next, use
vi
editor to edit the file
/etc/inittab
.
# vi /etc/inittab
Add the following line to the bottom of the file:
ntp : 2345 : respawn : /etc/init.d/fixtime
After you finish writing or modifying the code, remember to execute “umount /” to change the root directory
back to Read-only mode.
# umount /
Use the command
#init q
to re-initialize the kernel.
# init q