Skip to content

Instantly share code, notes, and snippets.

@nekoruri
Created July 2, 2015 06:15
Show Gist options
  • Save nekoruri/be078260fc13531ba611 to your computer and use it in GitHub Desktop.
Save nekoruri/be078260fc13531ba611 to your computer and use it in GitHub Desktop.
うるう秒ゆっくり同期していってね | ^o^  |
#!/bin/bash
UPSTREAMORIG_NTP_CONF=/etc/ntp.conf.upstream
NO_UPSTREAMORIG_NTP_CONF=/etc/ntp.conf.no_upstream
NTP_CONF=/etc/ntp.conf
# check
if [ ! -e $UPSTREAMORIG_NTP_CONF ]; then
echo "Not found $UPSTREAMORIG_NTP_CONF"
exit 1
fi
if [ ! -e $NO_UPSTREAMORIG_NTP_CONF ]; then
echo "Not found $NO_UPSTREAMORIG_NTP_CONF"
exit 1
fi
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
print_date() {
N=$1
echo $1 "$(date +%Y-%m-%dT%H:%M:%S) "
}
syslog() {
logger -t slew_batch -p user.info "$1"
}
syslog_and_echo() {
logger -t slew_batch -p user.info "$1"
print_date -n
echo "$1"
}
log_ntpdate() {
msg=$(ntpdate -q 210.173.160.27 | head -1)
syslog_and_echo "$msg"
}
# 1ループで15msぐらいずつ合わせる
# 無限ループで手で止める。
n=0
while true; do
n=$(($n + 1))
print_date -n
syslog_and_echo "[loop:$n]"
# 上位NTPサーバに向ける
syslog_and_echo "Connecting to MFEED"
log_ntpdate
sudo cp "$UPSTREAMORIG_NTP_CONF" "$NTP_CONF"
sudo /sbin/service ntpd restart
# 40秒まつ(0.5ms * 40s = 最大20msだけずらす: 実際はiburst同期で実績15msくらい)
wait=40
wait_notify_interval=10
wait_start=$(date +%s)
wait_notify=$wait_start
syslog_and_echo "Waiting $wait secs"
while [ $(($(date +%s) - $wait_start)) -le $wait ]; do
if [ $(($(date +%s) - $wait_notify)) -ge $wait_notify_interval ]; then
# $wait_notify_interval ごとにntpdate表示
# 時間かかるので先にwait_notify更新
wait_notify=$(date +%s)
log_ntpdate
fi
sleep 1
done
# sysclock => hwclock
syslog_and_echo "Sync to hwclock"
log_ntpdate
sudo /sbin/hwclock --systohc
# 上位NTPサーバを外しhwclockのみ見るようにする
syslog_and_echo "Disconnecting from MFEED. Only hwclock"
sudo cp "$NO_UPSTREAMORIG_NTP_CONF" "$NTP_CONF"
sudo /sbin/service ntpd restart
# 1024s(default max poll) + 60s(予備) = 1084s まつ
wait=1084
wait_notify_interval=100
wait_start=$(date +%s)
wait_notify=$wait_start
syslog_and_echo "Waiting $wait secs"
while [ $(($(date +%s) - $wait_start)) -le $wait ]; do
# こちらは待つだけなのでピリオド表示
if [ $(($(date +%s) - $wait_notify)) -ge $wait_notify_interval ]; then
# $wait_notify_interval ごとに経過秒数とntpdateを表示
echo "[$(($(date +%s) - $wait_start))]"
log_ntpdate
wait_notify=$(date +%s)
fi
# 長く待っても大丈夫なので10秒でsleep
sleep 10
echo -n "."
done
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment