Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to use NTPD to update my Linux machine's time to a specified NTP server.
Here is the scenario:

Each time the Linux machine starts up, I want to update the time from NTP server and if it's not successful, I want to try again every 5 minutes until successfully (max is 2 hours).

I searched around and find that I should(?) use NTPD and use some command like:

#ntpdate ntp.server.com (before starting NTPD)
#ntpd some_options_to_start

The questions are:

  1. How can I know if the time was successfully updated by these commands?
  2. Can I set the interval to update time from ntpd? (or I have to use something like sleep and loop with do..while/for in shell?)

Note that I want to execute the above commands in a shell script and will put the shell in a web server. Then clients (with a web browser browser) will execute the script on the website. So I need to check if the update is successful or not to send result to the client (over the web).

share|improve this question

4 Answers

up vote 6 down vote accepted

Using a script to monitor ntpd is not commonly done. Usually a monitoring tool like nagios or munin is used to monitor the daemon. The tool can send you an alert when things go wrong. I have munin emailing me if the offset exceeds 15 milliseconds.

Normally, you should use an odd number of servers so that the daemon can perform an election among the servers if one goes off. Three is usually adequate, and more than five is excessive. Clients on your internal network should be able to get by with one internal server if you monitor it. Use legitimate servers or your ISPs NTP or DNS servers as clock sources. There are public pools as well as public servers.

ntpd is self tuning and you should not need to adjust it once it is configured and started. With recent ntpd implementations you can drop use of ntpdate entirely as they can do the initial setting of the date.

The following script will parse the offsets in the output of ntpd and report an excessive offset. You could run it from cron to email you if there are problems. The script defaults to alerting on an offset of 0.1 seconds.

#!/bin/bash
limit=100   # Set your limit in milliseconds here
offsets=$(ntpq -nc peers | tail -n +3 | cut -c 62-66 | tr -d '-')
for offset in ${offsets}; do
    if [ ${offset:-0} -ge ${limit:-100} ]; then
        echo "An NTPD offset is excessive - Please investigate"
        exit 1  
    fi  
done
# EOF
share|improve this answer
This is the first instance I've heard of anyone monitoring system time. Excellent answer. – Bruce Ediger Aug 13 '11 at 17:20
@BillTHor: Great answer. Thanks a lot. Will try to apply it to my current work – sees Aug 16 '11 at 14:19

To answer the first question, ntpdate usually tells you what it did, or maybe did not do.

[root@flask rc.d]# ntpdate dagoo
12 Aug 10:04:03 ntpdate[20585]: adjust time server 10.0.0.15 offset -0.042285 sec

The NTP daemon, ntpd, runs constantly, and asks NTP servers (usually configured in /etc/ntp.conf) for the time every so often. You shouldn't have to run your script every 5 minutes. ntpdate should bring the machine into near synch with the server, and ntpd will run in the background and keep it in synch. You don't set the interval that ntpd tries, it adjusts the interval based on how it perceives the local clock drifts from the servers, and the quality of the connections to the servers.

You can use a program named ntpdc to see what ntpd keeps as information:

1 % ntpdc 
ntpdc> peers
     remote           local      st poll reach  delay   offset    disp
=======================================================================
*min-time-01.ine 10.0.0.15        1 1024  377 0.07047  0.014673 0.14360
=dns-01.esd189.o 10.0.0.15        2 1024  377 0.07587  0.022277 0.13660
ntpdc>

I think that the number you typically have an interest in is "offset", that's the number of seconds your local clock is off from the server's clock.

share|improve this answer
Great! But there is an unclear part in my question. I 'm going to execute a shell script in a C program. And wanted to check the return value (maybe I'll use the system("shellscript") function). Your answer gives me an idea that we should not set interval for NTPD and just in case, I want to change NTP server, I have to edit ntp.conf file. Could you tell me the way ntpd works with ntp server. Do I have to restart ntpd daemon after editing ntp.conf file (again using shell script) – sees Aug 12 '11 at 16:56
ntpd is a daemon process - it runs continuously. It decides how often to ask a server for current time, and how often and how much to change the local clock, based on how the local clock drifts: you can't really control any of the intervals. ntpd runs in the background. To change an NTP server, you edit /etc/ntp.conf, and stop then start ntpd. – Bruce Ediger Aug 12 '11 at 19:20
I should also mention that the code fragment you posted should get run at or above runlevel 3 during the boot. ntpdate sets the system clock, then ntpd becomes a daemon process, and keeps the clock synched to the servers. Ordinarily, you don't run those 2 lines of code to just "set the clock". – Bruce Ediger Aug 12 '11 at 22:25
I got it. How about ntpdate when it gets the wrong ntpserver ( for example) and don't work correctly. How can I know it from shell scripts? – sees Aug 13 '11 at 3:29

ntp-wait was made for this problem.

Five minutes with man ntp-wait and you should be up and running...

share|improve this answer

Chrony is claimed to handle your use case better than NTPd (network and machine on/off, suspend, etc.). See

http://fedoraproject.org/wiki/Features/ChronyDefaultNTP

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.