|[Home] [Credit Search] [Category Browser] [Staff Roll Call]||The LINUX.COM Article Archive|
|Originally Published: Tuesday, 11 January 2000||Author: Vorsprung|
|Published to: learn_articles_support/Articles||Page: 1/1 - [Printable]|
Time for Linux
One of the reasons we all use computers are that they are supposed to save us time. Most desktop setups have a place for a clock and most of us like to keep an eye on the time as we work. Below I will not be exploring how computers do not seem to save us time but how Linux keeps the time and date. I will also be looking at how to schedule commands automatically by time and how to synchronise your computers clocks to highly accurate atomic clocks on the Internet!
|Page 1 of 1|
Setting the Time
Linux keeps the time by generating a 'tick' hundreds of times a second and then using this to update a counter. It also keeps the date by keeping track of the number of seconds since 1st January 1970. This date is known as the Epoch and it is when the Unix time system began.
The most basic command for seeing the time is date. If you type date at a shell prompt then the Linux system will say something like:
It knows the time because it sets the counter.
To set the time and date with date is also easy:
It is usually a good idea to set the date with modified output from a prior date command.
While it is 2 a.m. in Cambridge, MA it is 7 a.m. in Cambridge, England. If there were Linux systems at both places they would have no problems deciding what time it really was. Linux, like all modern operating systems, uses a system of timezones that operates at an offset from Universal Central Time ( UTC ).
To alter the zone permanently on your Linux machine, there is usually a program like tzconfig on debian Linux. But most of us who aren't traveling the world with a laptop only need to do this once at install time then forget it.
When Linux isn't running the counters that keep track of the date and time off the kernel ticks do not run. So the computer keeps time using a low-powered clock that runs on battery. If your battery goes flat, or something else untoward occurs and strange dates or times come up when you boot Linux, you may need to adjust this hardware clock. The method I always use is to first set the correct date & time with date, and then issue the command
Which will force the battery-backed-up clock back to Linux time.
A Time, C time and M Time, 'Touch'ing Files
All files on Linux systems have time/date stamping on them. They have three types of stamp: create time, modify time and access time. These stamps are stored internally in the 'number of seconds since the Epoch' format, but you don't need to worry about this unless you are a programmer. Time/date stamping happens automatically with all operations. However, if the clock went wrong and you must have files with stamping to a particular time, then the touch command can alter the stamping arbitrarily.
will change the time/date stamp on filename to the current time.
Keeping in Sync
Do you have a cluster of machines which must share the same time, or do you simply want the time to be right? One solution is clock synchronization over the net.
xntp is available as a package on all modern Linux distributions. Be sure to select the xntp documentation package at the same time. You give xntp a machine to read the date from on the net, and the rest happens automatically.
To do this edit the /etc/ntp.conf file and add a line like
to the end, removing any other server lines. "ntp0.ja.net" is my local xntp server. See the directory in /usr/doc that the xntp documentation package made to find a xntp server near you.
You can also specify an internal machine to act as a time server. It is also possible to make a little hierarchy with one machine getting the correct time ( perhaps the Firewall box ) and then the others can read it as a server. There are full instructions on how to do this in the excellent documentation package, in HTML format.
Making it Happen
Often, now is not the time to look and see if that list of web pages has altered. 5 a.m. every night might be a better time but you are not in the office. Linux has a solution: it is called cron. Cron reads a file called crontab that specifies when a job will run and how to run it. Do:
This needs a little explanation. The 0 means minute zero. You can specify any minute 0 to 59. The 5 is 5 a.m.. It's 24 hour time spec so 17 would be 5 p.m. The rest of the fields specify day of the month, month and day of the week. The * in these positions mean 'every or any', so that's every day. If you only wanted to do this at the weekend you could say
6,0 means Saturday and Sunday. Commas are allowed to indicate multiple days to match. See man 5 crontab for more details. As for the wget command, it gets all the URLs listed in the file /home/james/URLS, but only if they have been altered, and tries three times at each before giving up.
There is another system for running a command once, called at. To use this say something like
Then enter the details of the commands you want to run at 5 a.m. and it will happen--if the machine is up of course. This is different from cron, as it only happens once. Both cron and at may need enabling for use for non-root users. Edit the
Accurate time is interesting when you have five minutes to catch that train but most of the time it just slips away. Install XDaliClock and watch time melt away . . .
|Page 1 of 1|