I am writing a data logging python script and I would like it to find out if the system clock has been synchronized via NTP recently, and if not, to attempt to cause it to happen before proceeding.
Starting from this answer, I've found that for the status
os.system('sudo service ntp status')
will display the results on the console, but I don't know how I can get the python script to see if ntp is active or not, since this call returns only
I could always just force a reset even if not needed using
os.system('sudo service ntp stop') os.system('sudo ntpd -gq') os.system('sudo service ntp start')
but it seems like a bad idea to do that if it wasn't necessary.
edit: using Raspbian GNU/Linux 8 (jessie)
Question: How can I test the status and then conditionally force a synchronization only if needed?
This answer suggests the package
ntplib https://pypi.python.org/pypi/ntplib/ but I'm not sure if this is advisable or not, so I thought I'd ask before installing and running it, and I'd still have to guess the status by comparing the results to the system clock and deciding if the agreement were good enough or not. I'd still like to know if ntp is active and that it believes the synchronization is close.