Question

I'm running Python (2.7.2) on an embedded linux system has roughly this startup sequence:

0) Reboot

1) Wait 2 minutes

2) Acquire network config via DHCP

A daemon process uses ntplib to acquire the current time and update its clock accordingly.

import ntplib  # http://pypi.python.org/pypi/ntplib/
...

    self.ntpClient = ntplib.NTPClient()
...

def getDate(self):

    try:
        logging.info('Sending NTP request to %s' % ('pool.ntp.org'))
        response = self.ntpClient.request('pool.ntp.org')
        secs = time.localtime(response.tx_time)
        logging.info('Response returned')
        return secs

    except Exception, e:
        logging.exception(e)
        return RESULT_FAILURE

A problem arises if the daemon process begins trying to resolve address info before the dhcp has run (step 2). This continues to be an error even after the DHCP client has run.

Here is the exception:

ERROR:[Errno 2] temporary failure in name resolution.
Traceback (most recent call last):
  File "/home/root/ntp_manager/ntp_manager.py", line 34, in getDate
    response = self.ntpClient.request('pool.ntp.org')
  File "/usr/lib/python2.7/ntplib.py", line 265, in request
    addrinfo = socket.getaddrinfo(host, port)[0]
gaierror: [Errno 2] temporary failure in name resolution.

However if the ntpClient makes its first attempt after the dhcp, then everything works fine.

It's as if the error is a product of some sort of caching (?!)

I've run out of ideas, some help would be much appreciated.

Thanks

Was it helpful?

Solution

I've worked out a solution that doesn't need any other libraries or scripts.

Scripts in the directory, /etc/network/if-up.d/ will be execute when a network interface is brought up. Here is a reference.

Therefore this ensures that my NTP script gets executed once the system has configured DHCP client and thereby to the internet. This the correct behaviour that avoids the name resolution problems that I ran into.

I've put together this simple script (and chmod +x it!) It checks that the network method is dhcp, and subsquently initializes my NTP daemon process.

#! /bin/sh
# ntp time-sync manager python daemon starter script

set -e

# ... Some Stuff Omitted Here ...

if [ "$METHOD" = dhcp ]; then
  echo -n "Starting $DESC..."
  start-stop-daemon --start --verbose --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS
  echo "METHOD: $METHOD"
  echo "IFACE: $IFACE"
fi

echo "Exiting if-up.d/ntp_manager"
exit 0

Here is a dump of what happens on the system. Please note the line: run-parts /etc/network/if-up.d being executed right after an IP has been resolved.

Reconfiguring network interfaces eth0 with DHCP... cat: can't open '/var/run/udh
cpc.eth0.pid': No such file or directory
run-parts /etc/network/if-pre-up.d
ifconfig eth0 up
udhcpc -R -b -p /var/run/udhcpc.eth0.pid -i eth0
udhcpc (v1.13.2) started
Sending discover...
Sending discover...
Sending select for 192.168.1.145...
Lease of 192.168.1.145 obtained, lease time 86400
adding dns 192.168.1.1
run-parts /etc/network/if-up.d
Starting NTP Time Manager...Daemon PID 1059
Redirecting serivice startup output to /home/root/Connect/log/ntp_manager_daemon.log
METHOD: dhcp
IFACE: eth0
Exiting if-up.d/ntp_manager

Anyway, I thought that this is a adequately simple and robust solution to the problem. Hopefully it will help someone else in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top