Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Denys Vlasenko | 24928ff | 2010-01-25 19:30:16 +0100 | [diff] [blame] | 3 | # Note that there is no provision to prevent several copies of the script |
| 4 | # to be run in quick succession. In fact, it happens rather often |
| 5 | # if initial syncronization results in a step. |
| 6 | # You will see "step" and then "stratum" script runs, sometimes |
| 7 | # as close as only 0.002 seconds apart. |
| 8 | # |
| 9 | # Script should be ready to deal with this. |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 10 | |
| 11 | dt=`date '+%Y-%m-%d %H:%M:%S'` |
| 12 | |
Denys Vlasenko | 1186894 | 2015-03-31 22:00:55 +0200 | [diff] [blame] | 13 | echo "`tail -n 199 -- "$0.log" 2>/dev/null`" >"$0.log.$$" |
| 14 | |
| 15 | if test x"$1" = x"unsync" \ |
| 16 | ; then |
| 17 | # No replies for our NTP requests were seen for some time. |
| 18 | # |
| 19 | # Among more mundate cases like network outages, this happens |
| 20 | # if we ran for a LONG time (days) and ntp server's IP has changed. |
| 21 | # ntpd has no code to re-resolve peers' addresses to IPs, |
| 22 | # we need to help it: |
| 23 | # |
| 24 | echo "$dt: $1"\ |
| 25 | "syncronization lost, restarting ntpd"\ |
| 26 | >>"$0.log.$$" |
| 27 | mv -- "$0.log.$$" "$0.log" |
| 28 | kill $PPID |
| 29 | exit |
| 30 | fi |
| 31 | |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 32 | if test x"$stratum" != x"" \ |
| 33 | && test x"$poll_interval" != x"" \ |
| 34 | && test 4 -ge "$stratum" \ |
| 35 | && test 128 -le "$poll_interval" \ |
| 36 | ; then |
| 37 | echo "$dt: $1"\ |
| 38 | "freq_drift_ppm=$freq_drift_ppm"\ |
| 39 | "offset=$offset"\ |
| 40 | "stratum=$stratum"\ |
| 41 | "poll_interval=$poll_interval,"\ |
Denys Vlasenko | 24928ff | 2010-01-25 19:30:16 +0100 | [diff] [blame] | 42 | "setting hardware clock"\ |
| 43 | >>"$0.log.$$" |
| 44 | mv -- "$0.log.$$" "$0.log" |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 45 | exec hwclock --systohc |
| 46 | fi |
| 47 | |
| 48 | echo "$dt: $1"\ |
| 49 | "freq_drift_ppm=$freq_drift_ppm"\ |
| 50 | "offset=$offset"\ |
| 51 | "stratum=$stratum"\ |
| 52 | "poll_interval=$poll_interval"\ |
Denys Vlasenko | 24928ff | 2010-01-25 19:30:16 +0100 | [diff] [blame] | 53 | >>"$0.log.$$" |
| 54 | mv -- "$0.log.$$" "$0.log" |