Linux Administrator's Handbook (9)--keeping Time_unix Linux

Source: Internet
Author: User
Tags local time
This chapter explains how Linux systems keeps time and what needs to be done to avoid problems. Usually, you don't have to do anything about time, but it's better to understand it.

Time

Time measurement is based on the most regular natural phenomena, such as the diurnal alternation caused by the rotation of the earth. The total diurnal time is constant, but the length of the day and night is varied. A simple constant is at noon.

Noon is the day when the sun is at its highest point. Since the earth is round, different places at noon occur at different times. This leads to the concept of local time locally.

Hardware clock and software clock

The PC has a battery-powered hardware clock. The battery is guaranteed to work at all times when the computer is out of power. Hardware can always be set from the BIOS setup screen or elsewhere in the operating system.

Linux core is independent of hardware always tracking time. At startup, Linux sets its own clock according to the hardware clock. Since then, 2 of them have been running independently of each other. Because viewing the hardware is always slow and complex, Linux manages its own clock.

The core always displays the universal time. In this way, the core does not need to know the time zone, and a highly reliable and simple result makes it easier to update time zone information. Each process processes the time zone transformation itself (using the standard tools in the time Zone package section).

Hardware can always be local time or universal time. It is usually better to use universal time because you do not need to change the hardware clock (UTC does not have DST) at the start or end of daylight savings. Unfortunately, some PC operating systems, including Msdos, Windows, and OS/2, assume that the hardware clock is local time. Linux can handle 2 different ways, but if the hardware clock displays local time, you must either start or end the daylight saving time (otherwise you cannot display local times).

Displaying and setting clocks

In a Debian system, the system time zone is determined by the symbolic connection/etc/localtime. The connection points to a time zone data file that describes the local time zone. The time zone data file exists in/usr/lib/zoneinfo. Other Linux distributions may be different.

The user can change his private time zone by setting the TZ environment variable. If not set, it is assumed to be the system time zone. The syntax of the TZ variable is described in the Tzset (3) man page.

The date command displays the current date and time. For example:

$ date
Sun June 21:53:41 EET DST 1996
$
That's Sunday, 14th of July, 1996, at about ten before ten on the evening, in the time zone called ' ' EET DST ' (whic H might be the European daylight savings Time). Date can also be used to display Universal Time:
$ date-u
Sun 18:53:42 UTC 1996
$
Date can also be used to set the core software always:
# date 07142157
Sun June 21:57:00 EET DST 1996
# date
Sun June 21:57:02 EET DST 1996
#

See the Date man page--syntax is a bit arcane in more detail. Only Root can set the time. Although each user can have its own time zone, but the clock is the same for everyone.

Date Displays or sets only the software clock. The clock command synchronizes hardware and software clocks. Used to read the hardware clock and set the software clock when the system starts. If two clocks need to be set, set the software clock first with date and then set the hardware clock with clock-w.

Clock-U tells it that the hardware clock is universal time. The-u option must be used correctly. Otherwise the computer will be puzzled exactly what time it is.

Clocks must be changed carefully. Many parts of the UNIX system require the clock to work properly. For example, the cron-waiting program runs the command periodically. If you change the clock, it may confuse whether it should run the command. On one early Unix system, someone set the clock twenty years into the future, and Cron wanted to run all periodic ANDs for twenty years all at once. Now the version of Cron can be handled correctly, but still be careful. Large and backward jumps are more dangerous than small.

When the clock was wrong

Linux software clocks are not always accurate. PC Hardware produces a time outage that runs the software clock. If the system is running too many processes, it takes too much time for the service to break, and the software clock starts back up. Hardware clocks run independently and are usually more accurate. If your system is constantly booting (for example, not most systems on the server), then the time is usually accurate.

If you need to adjust the hardware clock, it is usually easiest to reboot, go to the BIOS setup screen and do it there. This avoids all the problems that may result from changing the system time. If you cannot pass the BIOS, set the new time (in this order) with date and clock, but you must prepare to reboot if part of the system is not working properly.

A web-connected computer (even through a modem) can automatically check its own clock by comparing it to other computer times. If you know the other computers that maintain very precise time, then 2 computers will remain in precise time. This can be done using the rdate and Netdate commands. All 2 commands check the remote computer (Netdate can handle multiple remote computers) to synchronize the time of the local computer. By running such a program regularly, your computer can maintain the same exact time as a remote computer.

Measuring hole (measuring holes)

This appendix includes an interesting part of the program used to measure potential holes in the file system. The source distribution of the book contains the full source code (SAG/MEASURE-HOLES/MEASURE-HOLES.C).

int process (FILE *f, char *filename) {
static char *buf = NULL;
static Long prev_block_size =-1;
Long zeroes;
Char *p;

if (buf = NULL | | prev_block_size!= block_size) {
Free (BUF);
BUF = Xmalloc (block_size + 1);
Buf[block_size] = 1;
Prev_block_size = block_size;
}
Zeroes = 0;
while (Fread (buf, block_size, 1, f) = = 1) {
for (p = buf; *p = = '; ')
++p;
if (p = = buf+block_size)
Zeroes + = Block_size;
}
if (Zeroes > 0)
printf ("%ld%s\n", zeroes, filename);
if (Ferror (f)) {
ErrorMsg (0,-1, "Read failed for '%s '", filename);
return-1;
}
return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.