Index

Check Local Clock Against NTP Server

I wrote this script so I could keep an eye on an unstable NTP daemon.

The Code

#!/bin/bash
# time-offset_check.sh 
#
# Checks computer clock against specified NTP server. Issues a warning
# on stdout, if the time difference exceeds limit.  Requires ntpdate, bc
# and egrep in path. Run from cron as often as you like.

### Options

NTPSERVER='pool.ntp.org'   # i.e. random server from pool.ntp.org
LIMIT='1.000000'           # i.e. 1.000000 (= 1 second)

### Do not edit

OFFSET=`ntpdate -q $NTPSERVER \
	| egrep -o -m 1 -e "offset ((-)?[0-9]+\.[0-9]+)" \
	| egrep -o -e "[0-9]+\.[0-9]+"`

RESULT=`echo "$OFFSET >= $LIMIT" | bc`

if (("$RESULT" == 1)); then
	echo "Warning: Local clock offset ($OFFSET) larger than limit ($LIMIT)!"
fi

exit 0

Download

time-offset_check.sh


2009-04-12 Copyright Mattias Wikström (email: me at domain)