Discussion on clock and time in Linux

Source: Internet
Author: User
Tags symlink
Introduction to clock and time in Linux 1) Introduction to the basic concepts of clock in Linux 2) exploring how the hwclock command works. 3) synchronization between the system clock and the hardware clock in Linux during system startup. Www.2cto.com main terms and background knowledge UTC: Coord... introduction to clock and time in Linux 1) Introduction to the basic concepts of clock in Linux 2) exploring how the hwclock command works. 3) synchronization between the system clock and the hardware clock in Linux during system startup. Main terms and background knowledge of www.2cto.com UTC: Coordinated Universal Time, one is a piece of standard, used to regulate Time around the world. Time Zone: Time Zone, which indicates UTC-xx: xx, UTC + xx: xx. For example, the time zone in China is UTC +. We do not need to care about other related terms, such as CST and DST. A typical Linux System manages Clock and time. a typical Linux System has two types of Clock: System Clock and Hardware Clock ). The www.2cto.com hardware clock runs independently outside the operating system and has its own power supply. even when the system is shut down, it is still running. Hardware Clock is also called BIOS Clock, CMOS Clock, RTC, and so on. However, the word "hardware clock" is not easy to misunderstand. A system clock is a clock maintained by the operating system. In Linux, a clock is maintained by the kernel and driven by the interruption of the timer (because it is driven by the interruption of the timer, it can be considered as a software clock ). If there are two clocks, synchronization is required. This synchronization function is implemented by hwclock. Only a brief introduction is provided here. For more information, see the manual (man hwclock ). hwclock may work in different ways on different systems, but in most cases, it controls the hardware clock by accessing/dev/rtc. On the first release,/dev/rtc is often a symlink pointing to/dev/rtcX. There are two other factors that affect hwclock. one is the TZ environment variable and the other is the configuration file under/etc (the file location is different or may not exist, depending on the system ). In a typical Linux system, time synchronization automatically occurs when the system is started and the system is disabled. When the System starts, the content in the Hardware Clock is synchronized to the System Clock. when the System is disabled, the content in the System Clock is synchronized to the Hardware Clock. Of course, this kind of synchronization can also be done manually or by using scripts (self-written or self-contained in the system of this version ). The following is a typical script for synchronizing the system clock and hardware clock when the system is started and shut down. It usually exists in the/etc/init. d/directory. 01 #! /Bin/sh02 ### begin init INFO03 # Provides: hwclock04 # Required-Start: 05 # Required-Stop: $ local_fs06 # Default-Start: S07 # Default-Stop: 0 608 # Short-Description: Set system clock09 # Description: Set system clock to hardware clock, according to the UTC10 # setting in/etc/default/rcS (see also rcS (5 )). 11 ### end init INFO12 #13 # WARNING: If your hardware clock is not in UTC/GMT, this script14 # must know The local time zone. this information is15 # stored in/etc/localtime. this might be a problem if16 # your/etc/localtime is a symlink to something in17 #/usr/share/zoneinfo AND/usr isn' t in the root18 # partition! The workaround is to define TZ either19 # in/etc/default/rcS, or in the proper place below.20 21 [! -X/sbin/hwclock] & exit 022 23. /etc/default/rcS24 ["$ UTC" = "yes"] & tz = "-- utc" | tz = "-- localtime" 25 26 case "$1" in27 start) 28 if ["$ VERBOSE "! = No] 29 then30 echo "System time was 'date '. "31 echo" Setting the System Clock using the Hardware Clock as reference... "32 fi33 34 if [" $ HWCLOCKACCESS "! = No] 35 then36 if [-z "$ TZ"] 37 then38 hwclock $ tz -- hctosys39 else40 TZ = "$ TZ" hwclock $ tz -- hctosys41 fi42 fi43 44 if ["$ VERBOSE "! = No] 45 then46 echo "System Clock set. system local time is now 'date '. "47 fi48; 49 stop | restart | reload | force-reload) 50 #51 # Updates the Hardware Clock with the System Clock time.52 # This will * override * any changes made to the Hardware Clock.53 #54 # WARNING: If you disable this, any changes to the system55 # clock will not be carried into SS reboots.56 #57 if ["$ VERBOSE "! = No] 58 then59 echo "Saving the System Clock time to the Hardware Clock..." 60 fi61 if ["$ HWCLOCKACCESS "! = No] 62 then63 hwclock $ tz -- systohc64 fi65 if ["$ VERBOSE "! = No] 66 then67 echo "Hardware Clock updated to 'date'." 68 fi69 exit 070; 71 show) 72 if ["$ HWCLOCKACCESS "! = No] 73 then74 hwclock $ tz -- show75 fi76; 77 *) 78 echo "Usage: hwclock. sh {start | stop | show | reload | restart} "> & 279 echo" start sets kernel (system) clock from hardware (RTC) clock "> & 280 echo" stop and reload set hardware (RTC) clock from kernel (system) clock "> & 281 exit 182 ;; 83 although the -- utc option commands in esac hwclock and date have the -- utc option, their meanings are different. First, let's take a look at the command and result. 1chenqi @ chenqi-OptiPlex-760:/etc/default $ date2Wed Oct 31 16:00:23 CST 20123chenqi @ chenqi-OptiPlex-760:/etc/default $ date -- utc4Wed Oct 31 08:00:24 UTC 20125chenqi @ chenqi-OptiPlex-760: /etc/default $ sudo hwclock -- localtime-r6Wed 31 Oct 2012 08:00:30 am cst-0.594038 seconds7chenqi @ chenqi-OptiPlex-760: /etc/default $ sudo hwclock -- utc-r8Wed 31 Oct 2012 04:00:37 pm cst-0.937780 seconds visible from date -- u The time obtained by tc is the same as that obtained by hwclock -- localtime, while the time obtained by date is the same as that obtained by hwclock -- utc. This is easily confusing. In fact, the -- utc option in the date command indicates "operating on the standard UTC time", so date -- utc is the output of the current standard UTC time, while the -- utc option in hwclock indicates that, this operation should take into account the time zone conversion. for example, if you want to read the hardware clock, you must consider the time zone for the data to be read (from the local time, after calculating the time of the current time zone, make the output again. Similarly, -- localtime indicates that you do not need to consider and convert the time zone. if you read anything, you can output anything. As a result, the above troubles are easy to explain. The standard UTC time is used for reading the data stored in hardware clock, and the standard UTC time is obtained, that is, it is consistent with date -- utc. Another scenario is to consider the time zone conversion. Reference material hwclock manualdate manual http://en.wikipedia.org/wiki/Time_zone

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.