Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linux clock-setting script

5 views
Skip to first unread message

Paul Rubin

unread,
Jul 6, 2002, 5:16:55 AM7/6/02
to
I found Simon Foster's nice little SNTP client on the ASPN Cookbook
site and adapted it to set the hardware clock under Linux:

http://www.nightsong.com/phr/python/setclock.py

I find it pretty useful for adjusting my computer clock every
now and then, without the hassle of setting up a real NTP system.

William Park

unread,
Jul 7, 2002, 7:20:16 PM7/7/02
to

Thanks for the pointer, Paul. Exactly what I needed. Do you know any pure
shell script that does the same thing? I don't have Python installed on
every machine.

--
William Park, Open Geometry Consulting, <openge...@yahoo.ca>
8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin

Paul Rubin

unread,
Jul 7, 2002, 8:44:25 PM7/7/02
to
William Park <openge...@NOSPAM.yahoo.ca> writes:
> > http://www.nightsong.com/phr/python/setclock.py
> >
> > I find it pretty useful for adjusting my computer clock every
> > now and then, without the hassle of setting up a real NTP system.
>
> Thanks for the pointer, Paul. Exactly what I needed. Do you know any pure
> shell script that does the same thing? I don't have Python installed on
> every machine.

I don't know about shell but it should be simple to write the same
thing in Perl or even in C.

David LeBlanc

unread,
Jul 7, 2002, 9:26:30 PM7/7/02
to
"Set Your Clock Via Internet"
http://www.boulder.nist.gov/timefreq/service/its.htm

This particular page focuses on Windows, but the ftp site has sources in C.

David LeBlanc
Seattle, WA USA

> --
> http://mail.python.org/mailman/listinfo/python-list

William Park

unread,
Jul 7, 2002, 11:11:08 PM7/7/02
to

I've found a partial solution. I'm now using,
ntpdate time.apple.com time.windows.com time.nrc.ca ntp1.cmc.ec.gc.ca
where 'ntpdate' comes from NTP package. Still, shell solution would be
desireable, because I sometime don't have access to Perl, Python, or NTP
packages. But, I do have access to shell all the time.

I'll update when I find solution. :)

Paul Rubin

unread,
Jul 7, 2002, 11:35:21 PM7/7/02
to
William Park <openge...@NOSPAM.yahoo.ca> writes:
> I've found a partial solution. I'm now using,
> ntpdate time.apple.com time.windows.com time.nrc.ca ntp1.cmc.ec.gc.ca
> where 'ntpdate' comes from NTP package. Still, shell solution would be
> desireable, because I sometime don't have access to Perl, Python, or NTP
> packages. But, I do have access to shell all the time.
>
> I'll update when I find solution. :)

I didn't know about ntpdate. I don't know of any way to open sockets
from a shell script without something like perl, but maybe there is one.
Awk might be another possibility.

Paul Rubin

unread,
Jul 7, 2002, 11:38:30 PM7/7/02
to
"David LeBlanc" <whi...@oz.net> writes:
> "Set Your Clock Via Internet"
> http://www.boulder.nist.gov/timefreq/service/its.htm
>
> This particular page focuses on Windows, but the ftp site has sources in C.

I know there's a full blown NTP pckage on that ftp site but I didn't
want to spend hours reading documentation and configuring some hairy
monstrosity. I don't need millisecond accuracy; I just want my
computer to know what time it is within a few seconds.

Apparently the C package has a program called ntpdate that maybe does
the right thing, but I haven't examined it.

David LeBlanc

unread,
Jul 8, 2002, 1:36:35 AM7/8/02
to
No, the ftp site I pointed to has a simple client in C - admittedly with
some "windowisms" for setting the cmos clock on a PC mobo (which might
translate pretty directly over to linux). It's spread over several files and
looks to be about 20kb total of c source.

David LeBlanc
Seattle, WA USA

> -----Original Message-----
> From: python-l...@python.org
> [mailto:python-l...@python.org]On Behalf Of Paul Rubin
> Sent: Sunday, July 07, 2002 20:39
> To: pytho...@python.org
> Subject: Re: Linux clock-setting script
>
>

> --
> http://mail.python.org/mailman/listinfo/python-list

Paul Rubin

unread,
Jul 8, 2002, 2:30:07 AM7/8/02
to
"David LeBlanc" <whi...@oz.net> writes:
> No, the ftp site I pointed to has a simple client in C - admittedly with
> some "windowisms" for setting the cmos clock on a PC mobo (which might
> translate pretty directly over to linux). It's spread over several files and
> looks to be about 20kb total of c source.

Yeah, that's what I mean. I'd rather have 10 lines of Python than
20kb of C.

Dmitri I GOULIAEV

unread,
Jul 8, 2002, 2:52:26 AM7/8/02
to
Hi, Paul Rubin !

Or you can have just one line of shell command, like

# ntpdate host

which is (most probably) already in your distribution.
Do you have it in your distribution ? (just curious)

Best regards,

--
DIG (Dmitri I GOULIAEV)


Paul Rubin

unread,
Jul 8, 2002, 3:46:40 AM7/8/02
to
Dmitri I GOULIAEV <dmitri....@telkel.net> writes:
> Or you can have just one line of shell command, like
>
> # ntpdate host
>
> which is (most probably) already in your distribution.
> Do you have it in your distribution ? (just curious)

No, not in Red Hat 7.2 or 7.3. It's probably in the big NTP package
that's downloadable/installable, but as I said, I didn't feel like
figuring all that stuff out.

Peter Hansen

unread,
Jul 8, 2002, 8:37:00 AM7/8/02
to

I agree. Besides, the Python version is easier to make portable, which
I just did for my Windows 98 machines. (Thanks Paul!)

I changed the core of the "--set" conditional to this:

if os.name == 'posix':
os.system("/usr/sbin/hwclock --set '--date=%s'"% ct)
elif os.name == 'nt':
tt = time.localtime(t - TIME1970)
# warning: assumes M-D-Y (American) date format here!
winDate = '%s-%s-%s' % (tt[1], tt[2], tt[0])
winTime = '%s:%s:%s' % tt[3:6]
os.system("date %s" % winDate)
os.system("time %s" % winTime)
else:
print 'Unsupported OS %s, cannot set time.' % os.name

I also had to add a hash-bang line to the start, and change the
Linux path to /sbin/hwclock for my Linux boxes... Not sure that's
good, bad, or whether one should just rely on PATH to find the
executable.

-Peter

Simon Foster

unread,
Jul 8, 2002, 7:08:37 PM7/8/02
to
On 06 Jul 2002 02:16:55 -0700, Paul Rubin
<phr-n...@NOSPAMnightsong.com> wrote:

GEE Thanks!
--
Simon Foster
Cheltenham
England

William Park

unread,
Jul 9, 2002, 12:51:34 AM7/9/02
to
William Park <openge...@nospam.yahoo.ca> wrote:
> Paul Rubin <phr-n...@nospamnightsong.com> wrote:
>> William Park <openge...@NOSPAM.yahoo.ca> writes:
>>> > http://www.nightsong.com/phr/python/setclock.py
>>> >
>>> > I find it pretty useful for adjusting my computer clock every now and
>>> > then, without the hassle of setting up a real NTP system.
>>>
>>> Thanks for the pointer, Paul. Exactly what I needed. Do you know any
>>> pure shell script that does the same thing? I don't have Python
>>> installed on every machine.
>>
>> I don't know about shell but it should be simple to write the same thing
>> in Perl or even in C.
>
> I've found a partial solution. I'm now using, ntpdate time.apple.com
> time.windows.com time.nrc.ca ntp1.cmc.ec.gc.ca where 'ntpdate' comes from
> NTP package. Still, shell solution would be desireable, because I
> sometime don't have access to Perl, Python, or NTP packages. But, I do
> have access to shell all the time.
>
> I'll update when I find solution. :)

Found it!

For those of you who need to set system clock only periodically, say once a
day, here are methods that are available:

1. Network Time Protocol (RFC-1305, port 123):
ntpdate time.nist.gov ...

'ntpdate' is part of standard NTP package on any Linux distribution.

2. Time Protocol (RFC-868, port 37):
netdate time.nist.gov ...

Slackware includes 'netdate' as part of base TCP/IP package. You can
use 'rdist' from
ftp://ibiblio.org/pub/Linux/system/network/misc/
but it is now deprecated in favour of 'netdate'.

3. Daytime Protocol (RFC-867, port 13):
date -u -s `telnet time.nist.gov 13 | grep UTC | cut -f2,3 -d' '`

This is shell solution that I've been after.

Of course, one the system clock is set, you can set the CMOS clock by
hwclock --systohc

William Park

unread,
Jul 9, 2002, 11:49:46 AM7/9/02
to
William Park <openge...@nospam.yahoo.ca> wrote:
> Found it!
>
> For those of you who need to set system clock only periodically, say once a
> day, here are methods that are available:
>
> 1. Network Time Protocol (RFC-1305, port 123):
> ntpdate time.nist.gov ...
>
> 'ntpdate' is part of standard NTP package on any Linux distribution.
>
> 2. Time Protocol (RFC-868, port 37):
> netdate time.nist.gov ...
>
> Slackware includes 'netdate' as part of base TCP/IP package. You can
> use 'rdist' from

I meant 'rdate'... a typo.

Chris Gonnerman

unread,
Jul 9, 2002, 7:27:45 PM7/9/02
to
I use chrony for time maintenance. If your computer has intermittent
Internet
access (i.e. dialup) it is hands-down the best way to do it IMHO.

If this package has already been mentioned, sorry... I haven't followed this
thread closely.

http://chrony.sunsite.dk/

Off-topic since it's C, but I love it.

Chris Gonnerman -- chris.g...@newcenturycomputers.net
http://newcenturycomputers.net

William Park

unread,
Jul 9, 2002, 10:34:21 PM7/9/02
to
Chris Gonnerman <chris.g...@newcenturycomputers.net> wrote:
> I use chrony for time maintenance. If your computer has intermittent
> Internet access (i.e. dialup) it is hands-down the best way to do it
> IMHO.
>
> If this package has already been mentioned, sorry... I haven't followed
> this thread closely.
>
> http://chrony.sunsite.dk/
>
> Off-topic since it's C, but I love it.

I've seen it, but it's a tarball which you have to download and compile.
Whereas, 'netdate', 'ntpdate', and 'telnet 13' are ready to run on all
Linux distribution. I have DSL connection, so I run it once a day in
Crontab. If you dialup, then you can run it from '/etc/ppp/ip-up'.

Paul Rubin

unread,
Jul 9, 2002, 11:20:03 PM7/9/02
to
William Park <openge...@NOSPAM.yahoo.ca> writes:
> I've seen it, but it's a tarball which you have to download and compile.
> Whereas, 'netdate', 'ntpdate', and 'telnet 13' are ready to run on all
> Linux distribution.

Why do people keep saying that? Netdate and ntpdate are not on either
of the Red Hat boxes I use (one is RH 7.2 and one is 7.3).

Paul Rubin

unread,
Jul 9, 2002, 11:20:41 PM7/9/02
to
William Park <openge...@NOSPAM.yahoo.ca> writes:
> 3. Daytime Protocol (RFC-867, port 13):
> date -u -s `telnet time.nist.gov 13 | grep UTC | cut -f2,3 -d' '`
>
> This is shell solution that I've been after.

Woo hoo! I knew about RFC 868 but it didn't occur to me to use a
telnet command in a pipeline. I thought telnet had to interact with
the control tty.

Chris Gonnerman

unread,
Jul 9, 2002, 11:06:55 PM7/9/02
to
----- Original Message -----
From: "William Park" <openge...@rc.scd.yahoo.com>


> Chris Gonnerman <chris.g...@newcenturycomputers.net> wrote:
> > I use chrony for time maintenance. If your computer has intermittent
> > Internet access (i.e. dialup) it is hands-down the best way to do it
> > IMHO.
> >
> > If this package has already been mentioned, sorry... I haven't followed
> > this thread closely.
> >
> > http://chrony.sunsite.dk/
> >
> > Off-topic since it's C, but I love it.
>
> I've seen it, but it's a tarball which you have to download and compile.

This is a problem?

> Whereas, 'netdate', 'ntpdate', and 'telnet 13' are ready to run on all
> Linux distribution. I have DSL connection, so I run it once a day in
> Crontab. If you dialup, then you can run it from '/etc/ppp/ip-up'.

True. chrony doesn't update the time by "lurches" though, but by "skew,"
which is better in most cases.

To each his own.

William Park

unread,
Jul 10, 2002, 1:36:26 AM7/10/02
to
Chris Gonnerman <chris.g...@newcenturycomputers.net> wrote:
>> I've seen it, but it's a tarball which you have to download and compile.
>
> This is a problem?

Not really, but there are 2 other utilities in installation CD.

>
>> Whereas, 'netdate', 'ntpdate', and 'telnet 13' are ready to run on all
>> Linux distribution. I have DSL connection, so I run it once a day in
>> Crontab. If you dialup, then you can run it from '/etc/ppp/ip-up'.
>
> True. chrony doesn't update the time by "lurches" though, but by "skew,"
> which is better in most cases.

I think all does "skewing" if time difference is small enough (I think
0.5s).

Huaiyu Zhu

unread,
Jul 10, 2002, 2:10:35 PM7/10/02
to

The rpm package might be called ntpd or xntpd, or some other variant.

One problem with rpm is that if a file is not on the installed system, you
have to jump through some loops to figure out which package it is supposed
to come from. Hopefully the eventual CPyAN would be able to handle such
queries in an easy way.

Huaiyu

0 new messages