User`s manual
UC-8410/8416/8418-LX User’s Manual Managing Embedded Linux
3-11
Updating the Time Automatically
In this subsection, we show how to use a shell script to update the time automatically.
Example shell script to update 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. E.g., fixtime
How to run the shell script automatically when the kernel boots up
Copy the example shell script fixtime to directory /etc/init.d, and then use
chmod 755 fixtime to change the shell script mode. Next, use vi editor to edit the file /etc/inittab.
Add the following line to the bottom of the file:
ntp : 2345 : respawn : /etc/init.d/fixtime
Use the command #init q to re-init the kernel.
Cron—Daemon to Execute Scheduled Commands
Start Cron from the directory /etc/rc.d/rc.local. It will return immediately, so you do not need to
start it with an ampersand (&) to run in the background.
The Cron daemon will search /etc/cron.d/crontab for crontab files, which are named after
accounts in /etc/passwd.
Cron wakes up every minute, and checks each command to see if it should be run in the current
minute.
Modify the file /etc/cron.d/crontab to set up your scheduled applications. Crontab files have the
following format:
mm h dom mon dow user command
min hour date month week user command
0-59 0-23 1-31 1-12 0-6 (0 is Sunday)
The following example demonstrates how to use Cron.
The following steps show how to use cron to update the system time and RTC time every day at
8:00.
STEP1: Write a shell script named fixtime.sh and save it to /home/.
#!/bin/sh
ntpdate time.nist.gov
hwclock –w
exit 0