summaryrefslogtreecommitdiff
path: root/scripts/ntpd-set.sh
blob: 3581d2421b1574300d0aa2573a128f78af5551de (plain)
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
#!/bin/sh
max_attempts=4
try_count=0

# This script takes a list of ntp servers and passes them to ntpd to set the
# system time. If a /etc/ntpd.conf file exists, the servers there are used,
# if not, some default values are passed
set_time() {
    ntpd -n -p $1;
    return $?
}

check_time() {
    for arg ; do
        echo $arg
        if set_time $arg ; then
            return 0
        fi
    done
    # In case we are doing this before the network is up, try again
    let try_count=try_count+1
    if [ $try_count -lt $max_attempts ] ; then
        sleep 2
        check_time $@
    else
        return 1
    fi
}

if [ -f /etc/ntpd.conf ]; then
    server_list=`cat /etc/ntpd.conf | sed ':a;N;$!ba;s/\n/ /g;s/server//g'`
    check_time $server_list
else
    # Use a default list if there's no config
    check_time 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org
fi