Take the 2-minute tour ×
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

7 Answers 7

up vote 10 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
    
@BruceEdiger I take it you have never heard of the folks on the time-nuts mailing list. –  dfc Jan 22 at 17:21
    
As far as "Using a script to monitor ntpd is not commonly done" goes; the scripts directory inside of the ntp tarball points to the opposite conclusion. –  dfc Jan 22 at 17:24
    
@dvc The scripts there don't seem to include the requested functionality. There seems to be some code to generate SNMP traps, but I haven't encountered SNMP to monitor NTP. I've had to do my own monitoring in several large organizations. –  BillThor Jan 23 at 0:30

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

Use ntpstat.

myserver # ntpstat
synchronised to NTP server (10.89.160.13) at stratum 4
   time correct to within 124 ms
   polling server every 1024 s
share|improve this answer

I added to the @BillTHor bash script also a check for the ntpdstat exit code > 0:

#!/bin/bash
ntpstat > /dev/null
if [ $? -ne 0 ]; then
        echo "NTPD not synchronized - Please investigate"
        exit 1
fi
limit=1000   # 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

[UPDATE] since the script using ntpq output was unuseful for larget offset (more than 4 digits of offset) I tried a new version using only ntpstat:

#!/bin/bash
ntpstat > /dev/null
if [ $? -gt 0 ]; then
        echo "NTPD not synchronized - Please investigate"
        exit 1
fi
limit=1000   # Set your limit in milliseconds here
#offsets=$(ntpq -nc peers | tail -n +3 | cut -c 62-66 | tr -d '-')
#offsets=$(ntpq -nc peers | tail -n +3 | tr -s ' ' | cut -d ' ' -f 9 | tr -d '-' |tr -d '.')
offsets=$(ntpstat | tail -n +2 | head -n 1 | cut -c 27- | tr -d ' ms')
for offset in ${offsets}; do
    if [ ${offset:-0} -ge ${limit:-100} ]; then
        echo "NTPD offset is excessive: ${offset:-0} [ms] > ${limit:-100} [ms] - Please investigate"
        exit 1
    fi
done
# EOF`enter code here`
share|improve this answer
#!/usr/bin/bash
#set -x
NTPLIMIT=100   # Set your NTPLIMIT in milliseconds here
offsets=$(ntpq -nc peers | tail -3 | cut -c 62-66 | tr -d '-')
for offset in ${offsets}; do
    if [ ${offset:-0} -ge ${NTPLIMIT:-100} ]; then
        echo "An NTPd offset is excessive Please investigate" exit;
        else
                echo "NTP is within 0.1 second of time server"
                exit;
        fi
done

Same as the previous answer above, but with a slight modification as the previous command will run the if statement for as many offsets, i.e. if offset is 3 then it will print NTP is within 0.1.... 3 times before closing. Could be annoying if you have a server that is a long way out of sync. There is probably a way to remove the for loop as well...

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

RE why do I think chronny is good: It came pre-installed on my fedora machine and I've never had any issue with it (used it for years now). I've never had issues with ntpd in the past as well but if you read on the link I have provided, there is some information as to why chrony is better for non-always on machines. That's why I suggested to the op to try it out, it may or may not work better for him. So it's just another good option to try before going into too much tuning, optimizing and hacking ntpd.

share|improve this answer
    
please comment if you downvote with your rational –  akostadinov Jan 21 at 7:35
    
Why do you think chrony is better? –  dfc Jan 22 at 17:25
    
@dfc, it came pre-installed on my fedora machine and I've never had any issue with it (used it for years now). I've never had issues with ntpd in the past as well but if you read on the link I have provided, there is some information as to why chrony is better for non-always on machines. That's why I suggested to the op to try it out, it may or may not work better for him. So it's just another good option to try before going into too much tuning, optimizing and hacking ntpd. –  akostadinov Jan 23 at 10:03
    
Rather add this info on the Answer itself. It is valuable enough, and you just might get the vote reversed. –  Tshepang Feb 20 at 10:31

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.