LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting Software clock to Match Hardware clock (https://www.linuxquestions.org/questions/linux-newbie-8/setting-software-clock-to-match-hardware-clock-4175486843/)

T-Dub116 12-04-2013 10:43 AM

Setting Software clock to Match Hardware clock
 
Linux Redhat:

when my system is running harder usually during backups and what not, the time will slowdown with it and I have to keep changing it in order for it to stay accurate.

How can I set the Software clock to match the Hardware clock so that it is always correct?

something in Cron maybe...

DavidMcCann 12-04-2013 10:49 AM

You could always set the computer to use the Network Time Protocol: regularly check on the internet.

T-Dub116 12-04-2013 10:58 AM

The time is Wed 12/4 11:57am and here is what the server says

# date
Wed Dec 4 11:33:40 EST 2013
# hwclock
Wed 04 Dec 2013 01:00:49 PM EST -0.016045 seconds

How do I edit the HW clock to be the right time and then have the software clock look at the HWclock...something like,

/sbin/hwclock --hctosys

Z038 12-04-2013 12:02 PM

I do it the other way around, usually, by synching the system clock to the nist time server, then set the hwclock from that:

Code:

# ntpdate time.nist.gov
# hwclock --systohc --utc

I think it gives me a more accurate time. But you could do it as you've specified like this:

Code:

# hwclock --set --date="2013-12-04 12:02:00"
# hwclock --hctosys

The above example of --set with --date is in the man page for hwclock.

T-Dub116 12-04-2013 01:02 PM

Quote:

Originally Posted by Z038 (Post 5075349)
I do it the other way around, usually, by synching the system clock to the nist time server, then set the hwclock from that:

Code:

# ntpdate time.nist.gov
# hwclock --systohc --utc

I think it gives me a more accurate time. But you could do it as you've specified like this:

Code:

# hwclock --set --date="2013-12-04 12:02:00"
# hwclock --hctosys

The above example of --set with --date is in the man page for hwclock.



If you do it the other way around, show me what you do for that...

Z038 12-05-2013 01:30 AM

I showed that already. It's the first code block in my previous post. I issue the ntpdate command to set the software clock from an internet time server, then issue the hwclock command to set the hardware clock from the system clock.

I have an ancient laptop with a bad battery that always loses the hardware clock setting when it is shutdown. I put those commands in a shell script and run it out of /etc/rc.local to reset the hardware clock whenever I boot. You could make an entry for it in root's crontab and run it as often as you like.

Code:

#!/bin/bash
ntpdate pool.ntp.org
hwclock --systohc --utc

By the way, the general recommendation (in the US) is to sync to pool.ntp.org rather than to time.nist.gov in order to reduce the demand on the NIST server.



Another thing you could do is set up an ntp server on some system on your local network, then have all your other systems sync to it.

T-Dub116 12-05-2013 08:08 AM

Quote:

Originally Posted by Z038 (Post 5075751)
I showed that already. It's the first code block in my previous post. I issue the ntpdate command to set the software clock from an internet time server, then issue the hwclock command to set the hardware clock from the system clock.

I have an ancient laptop with a bad battery that always loses the hardware clock setting when it is shutdown. I put those commands in a shell script and run it out of /etc/rc.local to reset the hardware clock whenever I boot. You could make an entry for it in root's crontab and run it as often as you like.

Code:

#!/bin/bash
ntpdate pool.ntp.org
hwclock --systohc --utc

By the way, the general recommendation (in the US) is to sync to pool.ntp.org rather than to time.nist.gov in order to reduce the demand on the NIST server.



Another thing you could do is set up an ntp server on some system on your local network, then have all your other systems sync to it.


What do you think about this?

I set the date time (system Time)

date +%T -s 08:00:00 #8 am for an example

Then do: hwclock -w # to match the hwclock with the date clock

Then put this in crontab to keep them in sync

45 22,01,02 * * * /sbin/hwclock --hctosys


.

Z038 12-05-2013 12:08 PM

Yes, that will work. However, the time value you set manually that way is not likely to be as accurate as what you would get from an ntp server. So if your server is connected to the internet or if you have an ntp server on your network that you can sync to, you could get a much more accurate time.

You don't need to specify +%T on the date command. That controls the output format of the date. It doesn't have any bearing on the -s date string input value.

T-Dub116 12-06-2013 08:28 AM

Quote:

Originally Posted by Z038 (Post 5076087)
Yes, that will work. However, the time value you set manually that way is not likely to be as accurate as what you would get from an ntp server. So if your server is connected to the internet or if you have an ntp server on your network that you can sync to, you could get a much more accurate time.

You don't need to specify +%T on the date command. That controls the output format of the date. It doesn't have any bearing on the -s date string input value.


The Server is connected to the Internet, I don't believe they have a ntp server. What would you recommend being that the server does connect to the internet?

Z038 12-06-2013 07:08 PM

I would do what I described above in post #6 at boot up and at least once per day via cron to ensure the hardware clock stays fairly accurate. Then periodically throughout the day I would have cron run just the ntpdate command to reset the system clock from an internet time source (e.g., pool.ntp.org). How often you should run ntpdate depends on how fast your system clock gets out of sync. There's no problem with running it once an hour or even every five minutes.

Alternatively, if your hardware clock stays pretty accurate and you want to keep the system clock in sync with it, but you don't need to synchronize with the outside world, then you could use cron to periodically issue the hwclock --hctosys command as often as you need to keep them in sync.

The most automatic way of keeping the system clock accurate is to run the ntpd service (daemon). It's not that hard to set up, and it will keep your system clock synchronized with the internet ntp time servers.


All times are GMT -5. The time now is 07:13 AM.