-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntpsync
executable file
·61 lines (48 loc) · 1.88 KB
/
ntpsync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
if [ $(id -u) -ne 0 ]; then
echo -e "Script must be run as root. Try 'sudo $0'\n"
exit 1
fi
# Location of logfile
LOGFILE="/var/log/ntplog.log"
if [ ! -f $LOGFILE ]; then
touch $LOGFILE
fi
# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock
maxDiffSec="2"
msgNoConnection="No connection to time-server"
msgConnection="Connection to time-server"
# Check for NTP connection
if ( ntpq -p | grep -q "^*" ); then
echo $msgConnection
echo "---------------------------------"
secHwClock=$(sudo hwclock --debug | grep "^Hw clock time" | awk '{print $(NF-3)}')
echo "HwClock: $secHwClock sec"
secSysClock=$(date +"%s")
echo "SysClock: $secSysClock sec"
echo "---------------------------------"
secDiff=$(($secHwClock-$secSysClock))
# Compute absolute value
if ( echo $secDiff | grep -q "-" ); then
secDiff=$(echo $secDiff | cut -d "-" -f 2)
fi
echo "Difference: $secDiff sec"
msgDiff="HwClock difference: $secDiff sec"
if [ "$secDiff" -gt "$maxDiffSec" ] ; then
echo "---------------------------------"
echo "The difference between Hw- and Sys-Clock is more than $maxDiffSec sec."
echo "Hw-Clock will be updated"
# Update hwclock from system clock
sudo hwclock -w
msgDiff="$msgDiff --> HW-Clock updated."
fi
if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgConnection") || [ "$secDiff" -gt "$maxDiffSec" ]; then
echo $(date)": "$msgConnection". "$msgDiff >> $LOGFILE
fi
else
# No NTP connection
echo $msgNoConnection
if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgNoConnection"); then
echo $(date)": $msgNoConnection" >> $LOGFILE
fi
fi