royong
11-06-2008, 10:24
Hi all,
The following is a quick script that can be used to synchronize time on your server with an atomic internet accessible clock
Let's setup the directories first
# cd /root
# mkdir myscripts
# cd myscripts
# mkdir time
Now for the executable itself
# cat > /root/myscripts/time/synctime.pl <<"EOF"
#! /usr/bin/perl
# /root/myscripts/time/synctime.pl
# --------------------------------------------------------------------
# Utility obtained from the internet
# Used as part of the following script
# /root/myscripts/time/time.sh
# --------------------------------------------------------------------
$ntpservers[1] = "time-a.timefreq.bldrdoc.gov";
$ntpservers[2] = "time-b.timefreq.bldrdoc.gov";
$ntpservers[3] = "nist1-sj.glassey.com";
$ntpservers[4] = "utcnist.colorado.edu";
$ntpservers[5] = "time-nw.nist.gov";
$ntpservers[6] = "nist1.datum.com";
$ntpservers[7] = "nist1.aol-ca.truetime.com";
print "perl - synctime.pl running...\n";
# now try to set the time to a good ntp server, try several if no result
$oldtime = time;
foreach $ntpserverX (@ntpservers) {
$|=1; # flush any output
$ntpserver = qx"rdate -s $ntpserverX";
if ($? == 0) { last; }
else {
print "BAD NTP SERVER= '$ntpserverX' - remove it as soon as possible.\n";
}
}
$timecor = time - $oldtime;
if ($timecor > 2) {
print "CLOCK CORRECTED - It was SLOW, corrected by +$timecor seconds\n";
}
else {
if ($timecor < (-2)) {
print "CLOCK CORRECTED - It was FAST, corrected by $timecor seconds\n";
}
else {
print "CLOCK NOT CORRECTED - No adjustment needed\n";
}
}
EOF
Some simple permissions and adding 2 daily cron jobs to run at 0800hrs and 2000hrs respectively
# chmod +x /root/myscripts/time/synctime.pl
# echo "00 8 * * * root /root/myscripts/time/synctime.pl > /dev/null" >> /etc/crontab
# echo "00 20 * * * root /root/myscripts/time/synctime.pl > /dev/null" >> /etc/crontab
Now to test it out
# /root/myscripts/time/synctime.pl
# /root/myscripts/time/synctime.pl
# /root/myscripts/time/synctime.pl
The following is a quick script that can be used to synchronize time on your server with an atomic internet accessible clock
Let's setup the directories first
# cd /root
# mkdir myscripts
# cd myscripts
# mkdir time
Now for the executable itself
# cat > /root/myscripts/time/synctime.pl <<"EOF"
#! /usr/bin/perl
# /root/myscripts/time/synctime.pl
# --------------------------------------------------------------------
# Utility obtained from the internet
# Used as part of the following script
# /root/myscripts/time/time.sh
# --------------------------------------------------------------------
$ntpservers[1] = "time-a.timefreq.bldrdoc.gov";
$ntpservers[2] = "time-b.timefreq.bldrdoc.gov";
$ntpservers[3] = "nist1-sj.glassey.com";
$ntpservers[4] = "utcnist.colorado.edu";
$ntpservers[5] = "time-nw.nist.gov";
$ntpservers[6] = "nist1.datum.com";
$ntpservers[7] = "nist1.aol-ca.truetime.com";
print "perl - synctime.pl running...\n";
# now try to set the time to a good ntp server, try several if no result
$oldtime = time;
foreach $ntpserverX (@ntpservers) {
$|=1; # flush any output
$ntpserver = qx"rdate -s $ntpserverX";
if ($? == 0) { last; }
else {
print "BAD NTP SERVER= '$ntpserverX' - remove it as soon as possible.\n";
}
}
$timecor = time - $oldtime;
if ($timecor > 2) {
print "CLOCK CORRECTED - It was SLOW, corrected by +$timecor seconds\n";
}
else {
if ($timecor < (-2)) {
print "CLOCK CORRECTED - It was FAST, corrected by $timecor seconds\n";
}
else {
print "CLOCK NOT CORRECTED - No adjustment needed\n";
}
}
EOF
Some simple permissions and adding 2 daily cron jobs to run at 0800hrs and 2000hrs respectively
# chmod +x /root/myscripts/time/synctime.pl
# echo "00 8 * * * root /root/myscripts/time/synctime.pl > /dev/null" >> /etc/crontab
# echo "00 20 * * * root /root/myscripts/time/synctime.pl > /dev/null" >> /etc/crontab
Now to test it out
# /root/myscripts/time/synctime.pl
# /root/myscripts/time/synctime.pl
# /root/myscripts/time/synctime.pl