1836993 Members
2355 Online
110111 Solutions
New Discussion

NTP With Offset

 
Foo Cheek Chiang
New Member

NTP With Offset

Hi

Does anyone know if there is a way to synch server time using NTP and at the same time introduce an offset in the timing?

So I can be sure one of the server is always exactly 5 minutes behind the other servers when all are sync using NTP

3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: NTP With Offset

Wow! This is a strange request. I can think of a few schemes that might work. e.g. calling ntpq to query the time servers and then making a call to the adjtime() system call.

Do you really want the system time off by 5 minutes? That is really considered a big no-no especially in NFS or rdist environments. A better answer might be to create a custom TZ variable to control how time is DISPLAYED. That would be much more sensible and safe.

Perhaps if you explained what you are trying to do, someone could offer a better solution. I have a hard time believing that lying to a system about the time is ever a good idea.
If it ain't broke, I can fix that.
Foo Cheek Chiang
New Member

Re: NTP With Offset

Hi

Yeah I know it is kinda of a weird request.

I need to do it this way cause i got 2 different application running on the 2 machines.

If the application on machine A sends a message with a timestamp that is that is about the same as the time on machine B, the application puts the message in a pending queue and wont come back to it till an hour later.

If the timestamp is later than the time on machine B, it is processed immediately. It's a screw up logic but thats why I need to make sure that one of the machine is always 5 minutes behind the other and still synch with the NTP server. I need the NTP synch because I cant let the time gap get too big and the 2 machines need to be in synch with another couple of servers


A. Clay Stephenson
Acclaimed Contributor

Re: NTP With Offset

Okay, here is a screwball method that should work reasonably well. First, you need to disable the NTP client services on the box that is to run 5 minutes behind. Next, you should cron a job to execute this every 12 hours or so on the box that runs 5 minutes behind. (I haven't bothered to do all the PATH exports that a cron would require).

#!/usr/bin/sh

STAT=0
REMOTETIMESOURCE="remhost"
OFFSET=300 # seconds

REMOTE_SECONDS=$(remsh ${REMOTETIMESOURCE} perl -e \'print time\;\')
LOCAL_SECONDS=$(perl -e 'print time;')
ADJ=$((${REMOTE_SECONDS} - ${LOCAL_SECONDS} - ${OFFSET}))
if [[ ${ADJ} -lt 1 || ${ADJ} -gt 1 ]]
then
date -a ${ADJ}
STAT=${?}
fi
exit ${STAT}

This obviously can't keep you exactly 5 minutes apart but it should be able to keep you within 1 or 2 seconds of 5 minutes.

It does use the date -a command to slew the time rather than step adjust the time. You certainly don't want to step adjust your system time. Don't be tempted to run this every 5 minutes or so because you want to allow enough time for the last date -a slew adjustment to finish. Man date for details.


If it ain't broke, I can fix that.