summaryrefslogtreecommitdiff
path: root/poll-ntpd.sh
blob: 4fc8ece21a8db409da26748b383189cc19411752 (plain) (blame)
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
#!/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