summaryrefslogtreecommitdiff
path: root/poll-ntpd.sh
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-02-17 13:40:49 -0700
committerJosh Rahm <joshuarahm@gmail.com>2023-02-17 13:40:49 -0700
commitcc05bb2fc537ca0667730d8435e537e0ff2048cc (patch)
tree11fa7891a063e2c1517708b3651e65bca5bfdb45 /poll-ntpd.sh
downloadntp-module-main.tar.gz
ntp-module-main.tar.bz2
ntp-module-main.zip
initial commitHEADmain
Diffstat (limited to 'poll-ntpd.sh')
-rwxr-xr-xpoll-ntpd.sh59
1 files changed, 59 insertions, 0 deletions
diff --git a/poll-ntpd.sh b/poll-ntpd.sh
new file mode 100755
index 0000000..4fc8ece
--- /dev/null
+++ b/poll-ntpd.sh
@@ -0,0 +1,59 @@
+#!/bin/bash
+
+# This script waits for the network to come online and then will set the system time.
+#
+# This is useful because my system motherboard does not save the system time
+# for some reason.
+
+timeout=600
+count=0
+
+# Make sure we can resolve DNS addresses
+echo "nameserver 8.8.8.8" > /etc/resolv.conf
+
+# Wait for the given interface to come up and for a route to
+# be added.
+interface="$1"
+echo -n "Waiting for interface $interface ... "
+while ! ( ip route list dev "$interface" &>/dev/null ) ; do
+ if [[ "$count" -eq "$timeout" ]] ; then
+ echo "Timedout waiting for $interface. Exiting."
+ exit 1
+ fi
+
+ echo -n "."
+ sleep 0.1
+ count=$((count + 1))
+done
+echo -e "\ndone"
+
+# Even though a route has been added, it still takes time for DNS
+# to be come resolvable. Wait till we can ping one of the NTP
+# servers.
+echo -n "Waiting for 1.gentoo.pool.ntp.org to become reachable ... "
+timeout=10
+count=0
+
+while ! ( ping -c 1 1.gentoo.pool.ntp.org ) ; do
+ if [[ "$count" -eq "$timeout" ]] ; then
+ echo "Timedout waiting for 1.gentoo.pool.ntp.org. Exiting."
+ exit 1
+ fi
+
+ echo -n "."
+ sleep 1
+ count=$((count + 1))
+done
+echo -e "\ndone"
+
+# Finally, set the time.
+echo -n "Setting time ... "
+ntpd -I "$interface" -n -q
+
+if [[ "$?" -ne 0 ]] ; then
+ exit 1
+fi
+echo "done"
+
+# Touch a file to notify other scripts that the time has been set.
+touch /var/ntpd-time-set.target