Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Time setting on startup with no Internet?

Sun Jun 05, 2016 1:35 pm

How is the RPi setting its clock on bootup if there is no Internet connection?
Several cases:
1) Power outage and RPi starts up afterwards before Internet gateway is ready
2) RPi moved to location with no Internet
3) RPi has an RTC unit installed, does it care at all about Internet time?
4) If the RTC is not that good and deviates from true time, does the RPi reset the RTC to correct time if Internet time is available or will it run exclusively on the RTC?

I am asking because I thought the RPi would start back in 1970 and move to current time only after getting Internet connection, but last night we had a power outage in the office and my RPi started when power returned. This RPi does not have an RTC module installed.
At that time the ISP had no Internet connection for several hours. This particular Pi has a cron job to check for connection to a specific WiFi network so it logs the state every minute and on the logline is a timestamp too.
I can see that when the outage happens the next log line is predated to some time before the outage

Code: Select all

2016-06-04 02:05:01 No connection to WiFi
2016-06-04 02:06:01 No connection to WiFi
2016-06-04 01:18:01 No connection to WiFi  <== Back to earlier time (-48 min)! Power loss.
2016-06-04 01:19:01 No connection to WiFi
...
2016-06-04 18:47:01 No connection to WiFi
2016-06-04 18:48:01 No connection to WiFi
2016-06-04 20:25:30 No connection to WiFi <== Another jump, forward by 37m29s
2016-06-04 20:26:01 No connection to WiFi
It looks like the RPi returns to some earlier time when restarting without Internet but not all the way back to 1970. What is the rule here?
Then when Internet is restored after some 18 hours it looks like the RPi syncs to correct network time.

This RPi runs a version of Raspbian updated about 6 weeks ago.
Bo Berglund
Sweden

JimLill
Posts: 52
Joined: Sat Jan 04, 2014 2:09 pm

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 1:38 pm

I added an Adafruit GPS, gpsd, ntpd to my Pi just for this reason. You could simply add a RTC but I wanted very accurate time.

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 1:44 pm

Interesting with the GPS and ntpd.
I have experimented with a USB connected GPS unit on another RPi I have where I also installed gpsd (from source) to see what it could do. I did not use it for time setting though, just for position reading.

But if used for time setting I guess the unit needs a free view of the skies to get a satellite fix, right?
I am working on a system for logging data in the field (remote sites) and it needs fairly accurate time, so I will investigate how the addition of a GPS unit would be adapted. Depends on where the system is deployed regarding satellite connectivity.
Bo Berglund
Sweden

User avatar
rpdom
Posts: 15021
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 2:11 pm

Bosse_B wrote:How is the RPi setting its clock on bootup if there is no Internet connection?
Several cases:
1) Power outage and RPi starts up afterwards before Internet gateway is ready
2) RPi moved to location with no Internet
3) RPi has an RTC unit installed, does it care at all about Internet time?
4) If the RTC is not that good and deviates from true time, does the RPi reset the RTC to correct time if Internet time is available or will it run exclusively on the RTC?

I am asking because I thought the RPi would start back in 1970 and move to current time only after getting Internet connection, but last night we had a power outage in the office and my RPi started when power returned. This RPi does not have an RTC module installed.
At that time the ISP had no Internet connection for several hours. This particular Pi has a cron job to check for connection to a specific WiFi network so it logs the state every minute and on the logline is a timestamp too.
I can see that when the outage happens the next log line is predated to some time before the outage

Code: Select all

2016-06-04 02:05:01 No connection to WiFi
2016-06-04 02:06:01 No connection to WiFi
2016-06-04 01:18:01 No connection to WiFi  <== Back to earlier time (-48 min)! Power loss.
2016-06-04 01:19:01 No connection to WiFi
...
2016-06-04 18:47:01 No connection to WiFi
2016-06-04 18:48:01 No connection to WiFi
2016-06-04 20:25:30 No connection to WiFi <== Another jump, forward by 37m29s
2016-06-04 20:26:01 No connection to WiFi
It looks like the RPi returns to some earlier time when restarting without Internet but not all the way back to 1970. What is the rule here?
Then when Internet is restored after some 18 hours it looks like the RPi syncs to correct network time.

This RPi runs a version of Raspbian updated about 6 weeks ago.
If the Pi is using the normal fake-hwclock to keep track of the time, what it does is write the current time to a file when shutdown cleanly - and at 17 minutes past each hour (via /etc/cron.hourly/fake-hwclock). It then sets the clock to the time stored in that file until it is able to synchronise with ntp.

In your case, the time would have been logged at 01:17, then the Pi lost power sometime between 02:06 and 02:17 so the time when it restarted was set back to the last saved time of 01:17.

W. H. Heydt
Posts: 10772
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 2:12 pm

There are some inexpensive (< $3) RTC modules that include a battery backup available. Some have been tested to be good to within 30 sec. per year.

dgordon42
Posts: 760
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 2:30 pm

Bosse_B wrote:How is the RPi setting its clock on bootup if there is no Internet connection?
The Pi runs a fake hardware clock in software.

Simply put, at shutdown it writes the time into a file on the SD card. At the next boot up, it reads in this time, and uses it as the system time.

If you have an internet connection, it will use this to update the system time.

The fake hardware clock runs a script every hour (in /etc/cron.hourly) to update the time stored on the SD card, so it should never be out by more than one hour.

If you have a Real Time Clock, part of it's installation will disable the fake hardware clock, so the Pi will use the RTC at start up instead. Depending on it's set up, the RTC may also be updated by internet time when available.

From looking at your posted log, your Pi lost power sometime after 02:06 and crashed without updating the fake hardware clock. When power was restored, it booted up using the last saved fake hardware clock time. This appears as a 48 minute jump backwards in time. The internet connection was restored sometime after 18:48 'Pi time', and the system clock was updated to an accurate time, and time jumps forward by 37 minutes. So, I would say your Pi was without power for about 9 minutes.

Hope this helps,
Dave.

W. H. Heydt
Posts: 10772
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 3:34 pm

dgordon42 wrote: The fake hardware clock runs a script every hour (in /etc/cron.hourly) to update the time stored on the SD card, so it should never be out by more than one hour.
That assumes that the Pi is restarted quite soon after shutdown (for whatever reason). If the Pi is shutdown for, say several hours or several days, then the restored clock will be out for, at most, one hour plus the amount of time the Pi was not operating.

dgordon42
Posts: 760
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Time setting on startup with no Internet?

Sun Jun 05, 2016 3:51 pm

Yeah, I guess to be clear, the fake hardware clock stored time is the Pi's shutdown time. To guard against gross errors in this stored shutdown time, it is updated to the current time once per hour so that, in the event of a crash, it will have stored a shutdown time accurate to +0/-1 hour.

Dave.

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: Time setting on startup with no Internet?

Mon Jun 06, 2016 5:42 am

Thanks for the info on the fake clock.
Very interesting.
I will buy a few RTC units and put in critical RPi boxes since I cannot rely on the GPS coverage and also because GPS units are bulkier and more expensive.
Bo Berglund
Sweden

ejolson
Posts: 3424
Joined: Tue Mar 18, 2014 11:47 am

Re: Time setting on startup with no Internet?

Mon Jun 06, 2016 6:17 am

Bosse_B wrote:Thanks for the info on the fake clock.
Very interesting.
I will buy a few RTC units and put in critical RPi boxes since I cannot rely on the GPS coverage and also because GPS units are bulkier and more expensive.
Another solution for a Pi without internet connectivity would be to set up a local ntp server on a local area network using a PC that already has a RTC built in. Then change the configuration in /etc/ntp.conf so the Pi gets network time from the PC rather than from the internet.

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: Time setting on startup with no Internet?

Mon Jun 06, 2016 8:46 am

Well,
I am looking at a data collection system that will be running in a self-contained box out in the wilderness.
It is specified to use a mobile network router for Internet connectivity but that cannot be guaranteed.
So the time-stamps of the collected data need to be insured somehow with a backup time system inside the box.
Hence GPS or RTC seems to be the most viable solutions.

Another time related item:
I have designed an RPi box for use in a care home for my deaf mother-in-law.
It has a single purpose and that is to supply a big edit screen to the TV so visitors can type their messages and she can read.
This was done about a year ago or so. And it has no RTC nor any Internet connectivity at all.
Now on startup there is a message from the screensaver that it is out of date and needs to be fixed!
It was not there when I set it up, but I needed to ensure that there is no screensaver active because then it is impossible to switch the TV to show the RPi output.
How can the screensaver know it is out of date when there is no Internet available to check it?
And the time is just by dead reckoning from when it was deployed except for the down times when restarting.
I am very puzzled by this since I can't figure out any way the screensaver can know it is out of date..
Except if the developers have put an expire date right into the program itself of course. :(
Bo Berglund
Sweden

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Time setting on startup with no Internet?

Mon Jun 06, 2016 9:09 am

How can the screensaver know it is out of date when there is no Internet available to check it?
I have an application that has the fake HW clock disabled and uses a Dallas 3132 RTC chip for time keeping.

https://www.maximintegrated.com/en/prod ... S3231.html

Sometimes on startup, the pi does not initialize the I2C communication correctly with the RTC and the time is off. Because the fake clock is disabled, the time always sets to something like 1970 when it does not read the RTC. So, I wrote a Python script to look at system time on start up. If the year is < 1990 then it knows the time is off and it re-reads the time from the RTC. The main application is not allowed to start until the system time has been corrected.

Maybe that helps?

Henry

User avatar
Gerd
Posts: 66
Joined: Wed Mar 16, 2016 10:48 am
Location: Europe

Re: Time setting on startup with no Internet?

Mon Jun 06, 2016 12:02 pm

Bosse_B wrote: I am very puzzled by this since I can't figure out any way the screensaver can know it is out of date..
Except if the developers have put an expire date right into the program itself of course. :(
Which is exactly what happened: https://www.jwz.org/blog/2016/04/i-woul ... reensaver/
https://bugs.debian.org/cgi-bin/bugrepo ... bug=819703

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: Time setting on startup with no Internet?

Mon Jun 06, 2016 1:47 pm

Warning! Off Topic (or only barely on topic)...
OUCH!
The particular RPi this happens on is one I don't easily have access to...
I need to uninstall xscreensaver from it.
All other of my current RPi units do not use XScreenSaver because of this problem.
In fact I do not want any screensaver on my computers ever! We are not running on CRT:s with phosphor that burns out any longer, are we?
Beats me why people want screensavers in the first place...
Bo Berglund
Sweden

RPRPRPRPRP
Posts: 3
Joined: Fri Jun 30, 2017 6:48 am

Re: Time setting on startup with no Internet?

Fri Jun 30, 2017 7:41 am

W. H. Heydt wrote:There are some inexpensive (< $3) RTC modules that include a battery backup available. Some have been tested to be good to within 30 sec. per year.
Would you mind pointing me to the modules you mentioned? There are many available online and I want to be sure to get some with a reliable battery (not supercapacitor, unless it can hold the date 6 months+)! Thanks.

alphanumeric
Posts: 2015
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Time setting on startup with no Internet?

Fri Jun 30, 2017 12:00 pm

Bosse_B wrote:Warning! Off Topic (or only barely on topic)...
OUCH!
The particular RPi this happens on is one I don't easily have access to...
I need to uninstall xscreensaver from it.
All other of my current RPi units do not use XScreenSaver because of this problem.
In fact I do not want any screensaver on my computers ever! We are not running on CRT:s with phosphor that burns out any longer, are we?
Beats me why people want screensavers in the first place...
I install xscreensaver to stop my screen from auto blanking out. If I install it, then go into its settings and set it to disabled, my screen will never blank out or turn off. ;) Other wise my screen turns off after a few minutes of inactivity, which I find very annoying.

Bosse_B
Posts: 781
Joined: Thu Jan 30, 2014 9:53 am

Re: Time setting on startup with no Internet?

Fri Jun 30, 2017 1:18 pm

Is this a question?
Why restart discussions on a thread that is a year old?
Bo Berglund
Sweden

alphanumeric
Posts: 2015
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Time setting on startup with no Internet?

Fri Jun 30, 2017 2:41 pm

Apologies, I only saw the date on the last post before mine. :oops:

Return to “General discussion”