[maemo-users] [maemo-users] Handy script for using ntpdate

From: David D. Hagood wowbagger at sktc.net
Date: Sun Aug 13 18:59:14 EEST 2006
Here's a little script I knocked up to allow easy syncing of the Nokia's 
time to an NTP server.

It has the following prerequisites:

1) ntpdate must be installed.
2) the /usr/bin/ntpdate executable must be marked as SUID root.
3) /etc/sudoers must have the following line added:
user ALL = NOPASSWD: /mnt/initfs/usr/bin/retutime *

Given that, then this script will:

a) identify the current default gateway
b) Attempt to sync to an NTP server there (many gateways will be running 
NTPD so their log files can be correctly timestamped).
c) If that is successful, commit the new time to the RTC using the 
retutime command.
d) If that is NOT successful, then attempt to use one of the 
pool.ntp.org servers to sync.
e) if that is successful, then commit the time.

I've placed this command into /home/user/bin, and created an entry in 
the loadapplet command list for it - thus, I can connect, force a sync, 
and have the clock be set correctly.

Bonus points if somebody wanted to either a) hook this into the network 
connect scripts or b) hook in the "force network connection active" 
command into this.

# if no args give, re-run with the default route
if [ -z $1 ]
    exec $0 `/sbin/route | grep ^default`

# extract the default route destination

# OK, see if he is running a time server, and use him if so
echo Trying to sync to $gateway
if /usr/sbin/ntpdate -t .5 -u -b -p8 $gateway
    echo synced to $gateway
    sudo /mnt/initfs/usr/bin/retutime -I
    exit 0
# if that didn't work, try for a pool server
echo Trying to sync to pool.ntp.org
if /usr/sbin/ntpdate -u -b -p8 pool.ntp.org
    echo synced to pool.ntp.org
    sudo /mnt/initfs/usr/bin/retutime -I
    exit 0
exit 1

More information about the maemo-users mailing list