High Drift Time Synchronization

Introduction
You may want to periodically sync your system-clock with some external timeserver. If you want an accurate time, this article will not serve you well. You should then rather read NTP. This article is mostly for people whose system clocks get out of sync very rapidly due to a dying CMOS-Battery etc. and who can live with an inaccuracy of up to two minutes (worst case). On this site you will learn how to use WORLDTIMESERVER to set your system clock by utilizing the cron job manager and the provided python script.

The script provided on this page was inspired by a python-script written by Matthias Luebben. His script, which is no longer publicly available, is a little bit more complex but it allows you to define the format of the time that is returned by the script much in the same way that is possible with commands like date. It is, however, not possible to replace the script provided here with the original script and expect the proceeding instructions to work properly. The original script doesn't provide the appropriate formatting information, so I advise you to stick to the version of the script provided on this site unless you know what you are doing.

What You Need

 * The script provided on this site
 * A cron entry to run the script at specified intervals
 * A preferably permanent connection to the Internet

The Script
Save the following script onto a directory that is contained in the $PATH variable of user root. or even better are both good locations for this purpose. The following instructions assume that you have called the script, you may change it to your liking, and saved it into the directory. Keep this in mind when you enter the absolute path to the script and modify it to suit the location where you put, or whatever you called it, on your system. You can verify whether a directory is contained in $PATH by issuing:

/usr/local/sbin/ut.py

Remember that the script must be executable. You achieve this by running following command:

Testing the Script
Issue on the command line.

Output of ut.py

If the system clock is already accurate enough, the output will be like this:

Output of ut.py

You could now update the time of your system clock by issuing following command:

Or:

This would set the system's UTC (GMT) time to the value returned by ut.py. The UTC time may differ from your local time and that's perfectly alright because your system calculates the local time by adding or subtracting an offset to/from the UTC time. In Germany for example, setting the system clock to UTC time 21:48 results in the local time 22:48. The system takes care of that, so you don't need to worry about it. For UTC to work properly, remember to set the symbolic link to your location. The different location categories can be found under. To set the local time to that of Germany -- -- you would do following:

You can verify this with:

Output of ls -l /etc/localtime

The script decently gets the job done but always having to run this command in order to update the time is pretty tiresome and annoying. It would be nice to automate this task so it is carried out by the system auto-magically. Cron enables us to do just this. Read on to find out how this can be done.

Setting up cron the CLI
Since doing this requires a little bit of knowledge about cron, I refer you to the cron man/info pages and the following Gentoo guide that give you a first insight into cron: Gentoo Linux Cron Guide

A very simple setup that would sync the clock once every hour would be achieved like this: First you have to create a cron-file (with .cron as suffix) and copy it into the corresponding cron directory. For a script that is executed once every hour you would copy the cron-file to /etc/cron.hourly/.

create a file with following content in /etc/cron.hourly: /etc/cron.hourly/updatetime.cron

Make it executable with:

In order to have more control you will have to become more intimate with cron. In which case, you should read the above mentioned cron resources and Google for more tutorials. You may want to take a look at Kcron until you are more familiar with cron, or if you would prefer having a graphical interface to utilize.

Setting up cron using Kcron
Start Kcron as user root and click on the System Cron entry in the displayed TreeView. The System Cron entry has probably two sub-entries: tasks and variables.

What we are interested in is the Tasks entry. You need to right-click it and select New. This will create an new task and pop up a dialog in which you can set its properties. following Entries need to be modified:


 * 1) Run as --> set this to root
 * 2) Program --> set this to: /usr/local/sbin/ut.py
 * 3) The "Enabled" Checkbox must be checked.
 * 4) The "Run every day" entry in the "Daily" groupbox must also be checked.
 * 5) In the "Hours" & "Minutes" groupboxes you can set the intervals you wish the program to be executed at.

Now click ok and save the changes with the 'Save' button in the toolbar or by selecting File > Save from the menubar. This is important since the changes are not set to effect before you save them. Check to see whether the Run as setting is set right. My Kcron wouldn't set it correctly unless I closed and relaunched kcron and edited those entries again.

Troubleshooting
You can see whether the script was run correctly or not by checking the system logs. With metalog you find cron's logging information in.

Here is an example: Output of tailf /var/log/everything/current

AttributeError: 'NoneType' object has no attribute 'group'
Output

This was solved by a restructuring of the WORLDTIMESERVER page. The solution is to either change the URL in the urlopen function of the script to  http://www.worldtimeserver.com/current_time_in_UTC.aspx  instead of  http://www.worldtimeserver.com  or using the newest version of the script which already incorporates the fix.

Cron Entry Deleted
Updates to the system may delete the cron entry when the cron configuration files are overwritten. In case this happens, you have to re-create the cron entry.

Amarok, kaffeine or other players to stall for a few seconds every time the script is run.
The script may cause Amarok, kaffeine and other players to stall for a few seconds every time the script is run. This is the case when you are using the Xine-Engine for playback. Using GStreamer etc. on the other hand does not cause any problems. But since the script only modifies the system clock if the difference between the two is higher than two, this doesn't happen very often. Actually, it should only happen once every time you boot your system.